前言

在安装Postgresql数据库之前,需要先使用 brew list 命令查看之前是否安装过Postgresql。
例如之前安装过Postgresql 10.1版本,第二次又安装了Postgresql 11.2版本,会导致最后的时候数据库无法启动并报错:

“2019-05-09 19:16:31.378 CST [99652] DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.2.”

当存在两个版本冲突时,我采用的解决方式是直接使用brew uninstall postgresql命令删除安装的两个版本数据库,重新安装。

【当然,这种方式时候由于数据库本身没有重要数据,不建议,只因未用的时候暂未找到更好的方式】

安装命令

1、brew install postgresql -v
2、initdb /usr/local/var/postgres
3、brew services start postgresql

在Postgresql安装成功之后,会出现如下提示:

To restart postgresql after an upgrade:
brew services restart postgresql
Or, if you don't want/need a background service you can just run:
pg_ctl -D /usr/local/var/postgres start

提示说明可以使用brew services resstart postgresql启动Postgresql 服务或者使用 pg_ctl -D /usr/local/var/postgres start启动Postgresql,这里我使用的是第一个

Question1:
如果安装过Postgresql数据库,会存在/usr/local/var/postgres这样一个目录,再次安装数据库,使用initdb /usr/local/var/postgres 初始化数据库会出现如下提示,导致无法连接数据库终端

initdb: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

Answer:
可以将之前的目录移动到一个新的创建的目录下 mv /usr/local/var/postgres /usr/local/var/postgres1或者直接删除,再使用initdb /usr/local/var/postgres 命令重新初始化数据库

Result:

charodeacBook-Air:~ charo$ mv /usr/local/var/postgres /usr/local/var/postgres1  #
charodeMacBook-Air:~ charo$ initdb /usr/local/var/postgres
The files belonging to this database system will be owned by user "charolim".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

creating directory /usr/local/var/postgres ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start

charodeMacBook-Air:~ charo$

WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /usr/local/var/postgres -l logfile start

charodeMacBook-Air:~ charo$ psql
psql (11.2)
Type "help" for help.

charo=#

退出数据库可以使用\q

                                                 Datetime:2019.5.09 亲测

星梦天国
86 声望4 粉丝

只知道要努力,好像没有什么非做不可的事 温水煮青蛙 草稿纸进化成图纸