LoginSignup
9
12

More than 5 years have passed since last update.

PostgreSQL インストール、起動/停止、ユーザー作成、ロール作成

Last updated at Posted at 2018-01-02

実行環境

・CentOS7

・PostgreSQL 9.6

PostgreSQLのインストール

公式サイトのinux downloads (Red Hat family)に記載された手順を実行

・リポジトリRPMをインストール

yum install https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

・クライアントパッケージをインストール

yum install postgresql96

・SQLサーバーをインストール

yum install postgresql96-server

・DBの初期化

/usr/pgsql-9.6/bin/postgresql96-setup initdb

PostgreSQL起動/停止

・サービスに登録されているか確認

$ systemctl list-unit-files -t service | grep postgres
postgresql-9.6.service                        enabled 
$

・起動

systemctl status postgresql-9.6

起動が成功するとこんな感じで↓ログが出力される

$ systemctl status postgresql-9.6
● postgresql-9.6.service - PostgreSQL 9.6 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)
   Active: active (running) since 火 2018-01-02 08:59:40 UTC; 22min ago
     Docs: https://www.postgresql.org/docs/9.6/static/
  Process: 5749 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 5754 (postmaster)
   CGroup: /system.slice/postgresql-9.6.service
           ├─5754 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
           ├─5757 postgres: logger process   
           ├─5759 postgres: checkpointer process   
           ├─5760 postgres: writer process   
           ├─5761 postgres: wal writer process   
           ├─5762 postgres: autovacuum launcher process   
           └─5763 postgres: stats collector process   

・停止

systemctl stop postgresql-*

または

systemctl stop postgresql-9.6

このコマンドはroot権限で行う必要が有るため、パスワードを求められる

$ systemctl stop postgresql-*
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to manage system services or units.
Authenticating as: root
Password: 
==== AUTHENTICATION COMPLETE ===
$

・PostgreSQLのステータス確認:停止時

$ systemctl status postgresql-9.6
● postgresql-9.6.service - PostgreSQL 9.6 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since 火 2018-01-02 09:23:47 UTC; 13s ago
     Docs: https://www.postgresql.org/docs/9.6/static/
  Process: 5754 ExecStart=/usr/pgsql-9.6/bin/postmaster -D ${PGDATA} (code=exited, status=0/SUCCESS)
  Process: 5749 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 5754 (code=exited, status=0/SUCCESS)
 $  

・PostgreSQLのステータス確認:起動時

$ systemctl status postgresql-9.6
● postgresql-9.6.service - PostgreSQL 9.6 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-9.6.service; enabled; vendor preset: disabled)
   Active: active (running) since 火 2018-01-02 09:24:23 UTC; 3s ago
     Docs: https://www.postgresql.org/docs/9.6/static/
  Process: 6106 ExecStartPre=/usr/pgsql-9.6/bin/postgresql96-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 6111 (postmaster)
   CGroup: /system.slice/postgresql-9.6.service
           ├─6111 /usr/pgsql-9.6/bin/postmaster -D /var/lib/pgsql/9.6/data/
           ├─6114 postgres: logger process   
           ├─6116 postgres: checkpointer process   
           ├─6117 postgres: writer process   
           ├─6118 postgres: wal writer process   
           ├─6119 postgres: autovacuum launcher process   
           └─6120 postgres: stats collector process   
$  

・システム起動時にPostgreSQLが起動するようにする

$ systemctl enable postgresql-9.6
==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-unit-files ===
Authentication is required to manage system service or unit files.
Authenticating as: root
Password: 
==== AUTHENTICATION COMPLETE ===
==== AUTHENTICATING FOR org.freedesktop.systemd1.reload-daemon ===
Authentication is required to reload the systemd state.
Authenticating as: root
Password: 
==== AUTHENTICATION COMPLETE ===
$

ロールの設定

早速起動しようとするも、rootというロールは存在しないと怒られる

$ sudo psql
psql: FATAL:  role "root" does not exist

こちらの記事によると、PostgreSQLは権限の管理にロールを設定する必要があるらしい。

PostgreSQLは権限の管理にロール(role)が使われています。
ロールはUNIXのgroupに似ていて、個々のDBに対するアクセスまたは上書きの権限が設定されています。

〜中略〜

最初のインストールが出来たら、postgresというユーザかつ同名のロールが作られます。
デフォルトでは、postgresのロールはPostgreSQLにとってのスーパユーザであり、ロール設定を変える権限を持ちます。

というわけでまずはpostgresアカウントに切り替えて、PostgreSQLサーバにログイン

$ sudo su - postgres
-bash-4.2$ psql
psql (9.6.6)
Type "help" for help.

postgres=# 

ユーザ名とDB名を与えずにそのままpsqlコマンドを使えば、オペレーティングシステムのユーザと同名のユーザとDBが使われます。ですので上のpsqlのコマンドはpsql --username=postgres --dbname=postgresと同じ意味を持ちます。

まずは既存のロールを確認
\du または SELECT rolname FROM pg_roles;で確認できる

postgres=# SELECT rolname FROM pg_roles;
      rolname      
-------------------
 postgres
 pg_signal_backend
(2 rows)
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member
 of 
-----------+------------------------------------------------------------+-------
----
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

postgres=# 

デフォルトで存在するロールはスーパーユーザー権限を持つpostgresのみ。
ロールの作成方法および各権限の説明はここで確認できる

vagarntでpostgresqlを実行しているのでせっかくなのでvagrantユーザー&ロールを作成する

postgres=# CREATE USER vagrant;
CREATE ROLE
postgres=# select usename from pg_user;
 usename  
----------
 postgres
 vagrant
(2 rows)

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

postgres=# 

vagrantユーザーとvagrantロール(権限なし)を作成。
ロールに権限を付与する

postgres=# ALTER ROLE vagrant CREATEROLE CREATEDB LOGIN; 
ALTER ROLE
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member
 of 
-----------+------------------------------------------------------------+-------
----
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
 vagrant   | Create role, Create DB                                     | {}

postgres=# 

ロールvagrantにCREATEROLE、 CREATEDB、 LOGIN権限を付与

9
12
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
9
12