1. 程式人生 > >Mac上使用homebrew安裝PostgreSql 相關總結

Mac上使用homebrew安裝PostgreSql 相關總結

1. brew安裝命令

brew install postgresql

2. 檢視安裝的版本

pg_ctl -V

3. 安裝成功之後,

安裝路徑為:/usr/local/var/postgres

4. 初始化資料庫:

initdb /usr/local/var/postgres

5. 

配置開機登陸(可選):

$ mkdir -p ~/Library/LaunchAgents
$ cp /usr/local/Cellar/postgresql/9.3.5_1/homebrew.mxcl.postgresql.plist ~/Library/LaunchAgents/
$ launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

手動啟動 postgresql

$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

檢視狀態:

pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log status

停止:

$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log stop -s -m fast

6.

連線資料庫

1 psql -U username -d dbname -h 127.0.0.1

7.

檢視程序:

$ ps auxwww | grep postgres
june            56126   0.0  0.0  2432772    644 s000  S+    5:01下午   0:00.00 grep postgres
june            56058   0.0  0.0  2467360    584   ??  Ss    5:00下午   0:00.00 postgres: stats collector process
june            56057   0.0  0.0  2611808   1744   ??  Ss    5:00下午   0:00.00 postgres: autovacuum launcher process
june            56056   0.0  0.0  2611676    696   ??  Ss    5:00下午   0:00.00 postgres: wal writer process
june            56055   0.0  0.0  2611676    944   ??  Ss    5:00下午   0:00.01 postgres: writer process
june            56054   0.0  0.0  2611676    756   ??  Ss    5:00下午   0:00.00 postgres: checkpointer process
june            56044   0.0  0.2  2611676  14096 s000  S     5:00下午   0:00.02 /usr/local/Cellar/postgresql/9.3.5_1/bin/postgres -D /usr/local/var/postgres

8.解除安裝

$ brew uninstall postgres

如果配置了開機登陸:

$ launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
$ rm -rf ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

9.進入命令列模式:

$ psql -U test test_db -h localhost -W

如果出現 FATAL: Ident authentication failed for user,是因為:

This is because by default PostgreSQL uses ‘ident’ authentication i.e it checks if the username exists on the system. You need to change authentication mode to ‘trust’ as we do not want to add a system user. Modify the settings in “pg_hba.conf” to use ‘trust’ authentication.

請修改 /usr/local/var/postgres/pg_hba.conf 為:

host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust