LoginSignup
77
87

More than 3 years have passed since last update.

Ubuntu18.04にPostgreSQLをインストール

Last updated at Posted at 2017-02-10

※ 新しいバージョンに書き換えました

普段はメインのデータストアとしてMySQLを使用していますが、
9.0以降で実装された新たな機能(ストリーミグレプリケーション、JSONサポート)を体験したく、あらためてPostgresに触れてみようかと思っています。

新機能の所感はのちに触れるとして、今回はインストールからDB接続までを書いてみます。

バージョン

  • MacOSX El Capitan(10.11.6)
  • VirtualBox(5.1.8)
  • Vagrant(1.8.7)
  • Ubuntu(18.04 Bionic Beaver)
  • Postgres(12)

1. 仮想環境構築

こちらを参考に環境を作っておいてください。

2. パッケージのダウンロード一覧にPostgresを追記

$ sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ bionic-pgdg main' > /etc/apt/sources.list.d/pgdg.list"

3. 公開鍵を信頼キーリストに追加

$ wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -

念のため apt-key list で追加されたか確認しておきます。

$ apt-key list
/etc/apt/trusted.gpg
--------------------
pub   rsa4096 2011-10-13 [SC]
      B97B 0AFC AA1A 47F0 44F2  44A0 7FCC 7D46 ACCC 4CF8
uid           [ unknown] PostgreSQL Debian Repository

/etc/apt/trusted.gpg.d/ubuntu-keyring-2012-archive.gpg
------------------------------------------------------
pub   rsa4096 2012-05-11 [SC]
      790B C727 7767 219C 42C8  6F93 3B4F E6AC C0B2 1F32
uid           [ unknown] Ubuntu Archive Automatic Signing Key (2012) <ftpmaster@ubuntu.com>

/etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg
------------------------------------------------------
pub   rsa4096 2012-05-11 [SC]
      8439 38DF 228D 22F7 B374  2BC0 D94A A3F0 EFE2 1092
uid           [ unknown] Ubuntu CD Image Automatic Signing Key (2012) <cdimage@ubuntu.com>

/etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg
------------------------------------------------------
pub   rsa4096 2018-09-17 [SC]
      F6EC B376 2474 EDA9 D21B  7022 8719 20D1 991B C93C
uid           [ unknown] Ubuntu Archive Automatic Signing Key (2018) <ftpmaster@ubuntu.com>

問題なく追加されています。

4. パッケージを更新

$ sudo apt update

5. インストール実行

$ sudo apt install postgresql

6. 確認

psqlコマンド

$ which psql
/usr/bin/psql

postgresプロセス

apt からパッケージインストールした場合は自動起動されています。

$ ps aux | grep postgres | grep -v grep
postgres  4081  0.0  2.7 319648 27964 ?        Ss   17:14   0:00 /usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main -c config_file=/etc/postgresql/12/main/postgresql.conf
postgres  4092  0.0  0.3 319648  3960 ?        Ss   17:14   0:00 postgres: 12/main: checkpointer
postgres  4093  0.0  0.3 319784  3960 ?        Ss   17:14   0:00 postgres: 12/main: background writer
postgres  4095  0.0  0.9 319648  9684 ?        Ss   17:14   0:00 postgres: 12/main: walwriter
postgres  4097  0.0  0.6 320204  6804 ?        Ss   17:14   0:00 postgres: 12/main: autovacuum launcher
postgres  4098  0.0  0.4 174168  4280 ?        Ss   17:14   0:00 postgres: 12/main: stats collector
postgres  4099  0.0  0.6 320208  6812 ?        Ss   17:14   0:00 postgres: 12/main: logical replication launcher

設定ファイル

アクセス権
/etc/postgresql/12/main/pg_hba.conf

設定ファイル本体
/etc/postgresql/12/main/postgresql.conf

7. DBユーザーの登録

Postgresにはpostgresという管理ユーザーがいますが、
すべての権限が利用できてしまうので、ロールを限定したユーザーを別途作っておいた方がいいです。

postgresユーザーにスイッチ

$ sudo -u postgres -i

psqlコマンドでDBにアクセス

postgres@machine1:~$ psql
psql (12.3 (Ubuntu 12.3-1.pgdg18.04+1))
Type "help" for help.

postgres=#

ユーザーの確認

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

一旦抜ける

postgres=# \q

ユーザーの追加

今回はcreateuserコマンドでユーザー追加をしてみます。
もちろんSQLで追加することもできます。

postgres@machine1:~$ createuser --createdb --username=postgres --pwprompt [ユーザー名]

オプションの説明

  • --createdb : 新しいユーザーはデータベースの作成が許可される
  • --username : PostgreSQLに接続するユーザー名 (postgresを指定しているのでスーパーユーザーで接続)
  • --pwprompt : 新しいユーザーのパスワードを設定するためにプロンプトを表示

詳しくは createuser --help で確認してください。

今回は anonymous というユーザーを作ります。

postgres@machine1:~$ createuser -d -U postgres -P anonymous
Enter password for new role: 任意のパスフレーズを入力
Enter it again: 再度同じパスフレーズを入力

追加したユーザーの確認

postgres@machine1:~$ psql
postgres=# \du
                                   List of roles
 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 anonymous | Create DB                                                  | {}
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

DB作成の権限を与えられた anonymous ユーザーが登録されたことがわかります。

8. DBの作成

ここでもSQLではなく createdb コマンドを使ってDBを作成してみます。
postgresユーザーにスイッチしてください。

postgres@machine1:~$ createdb [DB名] --encoding=[エンコード] --lc-collate=[ロケール] --lc-ctype=[ロケール] --owner=[オーナー名] --template=template0

オプションの説明

  • --encoding : DBの文字コードを指定する(UTF-8, EUC-JP, LATIN1 etc...)
  • --lc-collate : 文字列の並び順を指定する(C, ja_JP.UTF-8, en_US,UTF-8 etc...)
    • 文字列ソートに影響
  • --lc-ctype : 文字の種類を指定する
    • 文字置換に影響(半角・全角)
  • --owner : オーナーを指定する
  • --template : ベースとなるDBを指定する
    • template0はまっさらなDBを作成し、template1の場合は複製される

詳しくは createdb --help で確認してください。

今回は sample_db というデータベースを作り、オーナーはさっき登録した anonymous を指定します。

postgres@machine1:~$ createdb sample_db --encoding=UTF-8 --lc-collate=C --lc-ctype=C --owner=anonymous --template=template0

作成したDBの確認

postgres@machine1:~$ psql
postgres=# \l
                                  List of databases
   Name    |   Owner   | Encoding |   Collate   |    Ctype    |   Access privileges
-----------+-----------+----------+-------------+-------------+-----------------------
 postgres  | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 sample_db | anonymous | UTF8     | C           | C           |
 template0 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |           |          |             |             | postgres=CTc/postgres
 template1 | postgres  | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |           |          |             |             | postgres=CTc/postgres
(4 rows)

sample_db という名前のデータベースが作成されていることがわかります。

9. 接続確認

追加したユーザーで作成したDBに接続してみます。

$ psql -U anonymous -h localhost -d sample_db

パスフレーズを聞かれるので入力すると無事に接続できます。

Password for user anonymous:
psql (12.3 (Ubuntu 12.3-1.pgdg18.04+1))
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

sample_db=>

以上でインストールから動作確認まで完了です。
次回はもう少し踏み込んで設定周りの調整と、新機能を試してみたいと思います。

77
87
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
77
87