1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mac+homebrew でPostgreSQL入門

Last updated at Posted at 2019-10-22

はじめに

この記事は備忘録として書いています。
悪い点は忌憚なくご指摘いただけると幸いです。

環境

Homebrew 2.1.15
psql (PostgreSQL) 11.5

PostgreSQLのインストールが完了した後

まず以下のコマンドでデータベースの一覧を確認してみる。
$ psql -l

k$ psql -l

                        List of databases
   Name    | Owner | Encoding | Collate | Ctype | Access privileges 
-----------+-------+----------+---------+-------+-------------------
 postgres  | k     | UTF8     | C       | C     | 
 template0 | k     | UTF8     | C       | C     | =c/k             +
           |       |          |         |       | k=CTc/k
 template1 | k     | UTF8     | C       | C     | =c/k             +
           |       |          |         |       | k=CTc/k
(3 rows)

データベースはデフォルトでこのようになっている。

PostgreSQLがデフォルトで参照するデータベースクラスタの設定

vimコマンドで~/.bash_profileというファイルへ
$ vi ~/.bash_profile
以下を追加する
export PGDATA=/usr/local/var/postgres

データベースの作成

psqldbというデータベースをpsqluserというロール名で作成する
$ createdb psqldb -O psqluser

$ psql -l

  List of databases
   Name    |  Owner   | Encoding | Collate | Ctype | Access privileges 
-----------+----------+----------+---------+-------+-------------------
 psqldb    | psqluser | UTF8     | C       | C     | 
 postgres  | k        | UTF8     | C       | C     | 
 template0 | k        | UTF8     | C       | C     | =c/k             +
           |          |          |         |       | k=CTc/k
 template1 | k        | UTF8     | C       | C     | =c/k             +
           |          |          |         |       | k=CTc/k
(3 rows)

psqldbというデーターベースにpsqluserというロールをオーナーとして入る
$ psql -U psqluser psqldb

// psqluser というロール名に postgres というパスワードを設定
psqldb=> alter role psqluser with password 'postgres';
ALTER ROLE

//ロール名の一覧を確認
paqldb=> \du
                                   List of roles
 Role name |                         Attributes                         | Member of 
-----------+------------------------------------------------------------+-----------
 k         | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 psqluser  |                                                            | {}

paqluserにはなんの権限もない状態なので権限を付与する

psqldb=> ALTER ROLE psqluser WITH CREATEDB;
ERROR:  permission denied

むむむ、エラーだ
一度kというユーザーとしてpsqldbに接続

$ psql -U k psqldb


psqldb=# ALTER ROLE psqluser WITH createdb;
ALTER ROLE
psqldb=# ALTER ROLE psqluser WITH createrole;
ALTER ROLE
psqldb=# ALTER ROLE psqluser WITH Superuser;
ALTER ROLE
psqldb=# \du
                                    List of roles
 Role name |                         Attributes                         | Member of  
-----------+------------------------------------------------------------+------------
 k         | Superuser, Create role, Create DB, Replication, Bypass RLS | {psqluser}
 psqluser  | Superuser, Create role, Create DB                          | {}

psqluser に無事権限を付与できた

よくあるエラー

 k$ postgres -D /usr/local/var/postgres
2019-10-22 23:04:38.695 JST [14260] FATAL:  lock file "postmaster.pid" already exists
2019-10-22 23:04:38.695 JST [14260] HINT:  Is another postmaster (PID 13895) running in data directory "/usr/local/var/postgres"?

正しく停止させないとpid情報が残ってしまう
以下を実行
rm /usr/local/var/postgres/postmaster.pid

2019-10-22 23:04:57.924 JST [14265] LOG:  listening on IPv6 address "::1", port 5432
2019-10-22 23:04:57.924 JST [14265] LOG:  listening on IPv4 address "127.0.0.1", port 5432
2019-10-22 23:04:57.926 JST [14265] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2019-10-22 23:04:57.950 JST [14266] LOG:  database system was interrupted; last known up at 2019-10-22 22:49:54 JST
2019-10-22 23:04:58.084 JST [14266] LOG:  database system was not properly shut down; automatic recovery in progress
2019-10-22 23:04:58.087 JST [14266] LOG:  redo starts at 0/167D3D0
2019-10-22 23:04:58.087 JST [14266] LOG:  invalid record length at 0/167D4B0: wanted 24, got 0
2019-10-22 23:04:58.087 JST [14266] LOG:  redo done at 0/167D478
2019-10-22 23:04:58.100 JST [14265] LOG:  database system is ready to accept connections

$ postgres -D /usr/local/var/postgres
もう一度実行
こんなログが出れば完了

参考文献

https://qiita.com/yh2020/items/8be3087004d100fe752b
http://ponsuke-tarou.hatenablog.com/entry/2018/01/31/232012
https://codenote.net/mac/homebrew/3894.html
https://codenote.net/mac/homebrew/187.html
https://teratail.com/questions/112476
https://qiita.com/sibakenY/items/407b721ad1bd0975bd00
http://db-study.com/archives/121

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?