Mac下安裝Postgresql數據庫

前言

在安裝Postgresql數據庫以前,須要先使用 brew list 命令查看以前是否安裝過Postgresql。
例如以前安裝過Postgresql 10.1版本,第二次又安裝了Postgresql 11.2版本,會致使最後的時候數據庫沒法啓動並報錯:sql

「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命令刪除安裝的兩個版本數據庫,從新安裝。數據庫

【固然,這種方式時候因爲數據庫自己沒有重要數據,不建議,只因未用的時候暫未找到更好的方式】bootstrap

安裝命令

一、brew install postgresql -v
二、initdb /usr/local/var/postgres
三、brew services start postgresql

在Postgresql安裝成功以後,會出現以下提示:post

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 startui

提示說明可使用brew services resstart postgresql啓動Postgresql 服務或者使用 pg_ctl -D /usr/local/var/postgres start啓動Postgresql,這裏我使用的是第一個this

Question1:
若是安裝過Postgresql數據庫,會存在/usr/local/var/postgres這樣一個目錄,再次安裝數據庫,使用initdb /usr/local/var/postgres 初始化數據庫會出現以下提示,致使沒法鏈接數據庫終端rest

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 命令從新初始化數據庫postgresql

Result:code

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=#

退出數據庫可使用\qorm

Datetime:2019.5.09 親測
相關文章
相關標籤/搜索