MacにPostgreSQLをインストールした後のメモ書き

デフォルトで以下のデータベースが作られる

データベース名 postgres
アカウント postgres

f:id:yossan2:20180721140716p:plain

コマンドは以下にインストールされる。(バージョン10の場合)

/Library/PostgreSQL/10/bin

terminalから簡単にコマンドを呼び出せるようにPathを追加

export PATH=/Library/PostgreSQL/10/bin:$PATH

新規アカウントの作成

新規アカウント (yossan)の作成

$ createuser -U postgres yossan
  • -U <account> 指定したaccountで、 createuser を実行

パスワードの設定

postgresアカウントでpostgresデータベースにログイン後、 yossanアカウントを alterコマンドで編集する。

$ psql -U postgres -d postgres
postgres=# alter user yossan with encrypted password 'test';
ALTER ROLE
  • -U <account> ログインで使用するアカウントを指定
  • -d <database> ログインするデータベースを指定

権限を superuserに変更

データベースなどを作成できるようになる

postgres=# alter role yossan with superuser;
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 yossan    | Superuser                                                  | {}

データベースの新規作成

testdbデータベースの作成例

$ createdb -U yossan testdb

testdbへのログイン

$ psql -U yossan testdb
Password for user yossan: 
psql (10.4)
Type "help" for help.

testdb=# 

PostgreSQLサーバーの起動

一旦ホームディレクトリに移行後、ログインユーザを postgres に変更する

$ cd 
$ sudo -u postgres bash
postgres$

起動

postgres$ pg_ctl start -D /Libaray/PostgreSQL/10/data
waiting for server to start....2018-07-21 14:32:47.174 JST [672] LOG:  listening on IPv6 address "::", port 5432
2018-07-21 14:32:47.174 JST [672] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2018-07-21 14:32:47.176 JST [672] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2018-07-21 14:32:47.196 JST [672] LOG:  redirecting log output to logging collector process
2018-07-21 14:32:47.196 JST [672] HINT:  Future log output will appear in directory "log".
 done
server started

停止

postgres$ pg_ctl stop -D /Library/PostgreSQL/10/data

参考

Creating user, database and adding access on PostgreSQL https://medium.com/coding-blocks/creating-user-database-and-adding-access-on-postgresql-8bfcd2f4a91e

How To Use Roles and Manage Grant Permissions in PostgreSQL on a VPS https://www.digitalocean.com/community/tutorials/how-to-use-roles-and-manage-grant-permissions-in-postgresql-on-a-vps--2

Switch to postgres user on Mac https://stackoverflow.com/questions/10753340/switch-to-postgres-user-on-mac

How to start PostgreSQL server on Mac OS X? https://stackoverflow.com/questions/7975556/how-to-start-postgresql-server-on-mac-os-x