在Mac OS X上安装Postgres

在mac上安装Postgres。我已经忘记了很多东西,所以写下来作为备忘录。

确认安装

$ brew search postgres
==> Searching local taps...
postgresql ✔        postgres-xc         postgresql@9.5      postgrest
check_postgres      postgresql@9.4      postgresql@9.6

如果没有安装的话

$ brew install postgresql

关联 brew

链接
$ brew link postgresql
Linking /usr/local/Cellar/postgresql/10.0... 666 symlinks created
确认initdb
$ which initdb | xargs readlink
../Cellar/postgresql/10.0/bin/initdb

顺便说一下,unlink是

$ brew unlink postgresql

版本

$ postgres --version
postgres (PostgreSQL) 10.0
$ psql --version
psql (PostgreSQL) 10.0

初始化数据库

$ initdb /usr/local/var/postgres -E utf8
The files belonging to this database system will be owned by user <USERNAME>.
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
initdb: could not find suitable text search configuration for locale "ja_JP.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

启动

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

或者

$ postgres -D /usr/local/var/postgres

停下来

$ pg_ctl stop -D /usr/local/var/postgres

启动服务

确认
$ brew services list
Name         Status  User Plist
postgresql   stopped
启动
$ brew services start postgresql
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)
停下来
$ brew services stop postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
重新启动
$ brew services restart postgresql
Stopping `postgresql`... (might take a while)
==> Successfully stopped `postgresql` (label: homebrew.mxcl.postgresql)
==> Successfully started `postgresql` (label: homebrew.mxcl.postgresql)

切换版本

$ pg_ctl stop -D /usr/local/var/postgres
$ brew switch postgres 9.6.3
$ initdb /usr/local/var/postgres96 -E utf8
$ brew services restart postgresql
$ pg_ctl -D /usr/local/var/postgres96 -l logfile start
广告
将在 10 秒后关闭
bannerAds