MacOS安装PostgreSQL

测试环境

  • macOS Sierra 10.12.6

安装 PostgreSQL

1
brew install postgresql

配置 PostgreSQL

查看版本

1
2
➜  ~ pg_ctl -V
pg_ctl (PostgreSQL) 10.3

安装成功之后,安装路径为:/usr/local/var/postgres

初始化数据库

1
initdb /usr/local/var/postgres

手动启动 PostgreSQL

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

查看 PostgreSQL 运行状态

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

手动停止 PostgreSQL

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

查看进程

1
ps aux | grep postgres

创建用户和数据库 (此处未测试)

1
2
3
4
5
6
#createuser will prompt you for a password, enter it twice.
$ createuser -U postgres -P 123456
$ createdb -O postgres -E utf8 test_db
$ psql
postgres=# GRANT ALL PRIVILEGES ON postgres TO postgres;
postgres=# \q

进入命令行模式

1
psql -U test test_db -h localhost -W

设置开机自启动

1
2
sudo ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

错误解决

FATAL: Ident authentication failed for user

1
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 为:

安装 pgadmin,下载地址:http://www.pgadmin.org/download/macosx.php

via:https://yq.aliyun.com/articles/25638