icon-attention デザイン新しくしました。

icon-calendar 2021年2月28日

【CentOS7】PostgreSQLのインストール

未分類

■リポジトリの追加

$ sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

■PostgreSQLのインストール

現在の最新バージョンを確認してからインストールします。

$ sudo yum list | grep postgresql
...
postgresql12-tcl.x86_64                   2.7.5-1.rhel7              pgdg12
postgresql12-test.x86_64                  12.6-1PGDG.rhel7           pgdg12
postgresql13.x86_64                       13.2-1PGDG.rhel7           pgdg13
postgresql13-contrib.x86_64               13.2-1PGDG.rhel7           pgdg13
postgresql13-devel.x86_64                 13.2-1PGDG.rhel7           pgdg13
postgresql13-docs.x86_64                  13.2-1PGDG.rhel7           pgdg13
postgresql13-libs.x86_64                  13.2-1PGDG.rhel7           pgdg13
postgresql13-llvmjit.x86_64               13.2-1PGDG.rhel7           pgdg13
postgresql13-odbc.x86_64                  13.00.0000-1PGDG.rhel7     pgdg13
postgresql13-plperl.x86_64                13.2-1PGDG.rhel7           pgdg13
postgresql13-plpython3.x86_64             13.2-1PGDG.rhel7           pgdg13
postgresql13-pltcl.x86_64                 13.2-1PGDG.rhel7           pgdg13
postgresql13-server.x86_64                13.2-1PGDG.rhel7           pgdg13
postgresql13-test.x86_64                  13.2-1PGDG.rhel7           pgdg13
postgresql95.x86_64                       9.5.25-1PGDG.rhel7         pgdg95
postgresql95-contrib.x86_64               9.5.25-1PGDG.rhel7         pgdg95
...

v13が最新のようです。
ちなみに、95とついているものはv95ではなくv9.5です。

$ sudo yum install -y posgresql13-server

インストール時にpostgresユーザーも作成されます。

■初期設定

postgresユーザーで/usr/pgsql-13/bin/initdbを実行します。

$sudo su - postgres -c '/usr/pgsql-13/bin/initdb -E UTF8 --locale=C -A scram-sha-256 -W'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "C".
The default text search configuration will be set to "english".

Data page checksums are disabled.

Enter new superuser password:
Enter it again:

fixing permissions on existing directory /var/lib/pgsql/13/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

Success. You can now start the database server using:

    /usr/pgsql-13/bin/pg_ctl -D /var/lib/pgsql/13/data -l logfile start

-A scram-sha-256は認証方式の設定です。
また、-Wをつけるとパスワード設定を行うウィザードが追加されます。

■サービスの起動

$ sudo systemctl start postgresql-13
$ sudo systemctl enable postgresql-13

■postgresユーザーの環境変数設定

MySQLとは違い、PostgreSQLはDB管理ユーザーにログインしてから操作します。
初期状態では操作コマンド等のPATHが通っていないので設定します。

このとき、.bash_profileはPostgreSQLの再インストールなどを行うと
上書きされる可能性があるため、.pgsql_profileという環境変数設定専用の
ファイルを作成することで、.bash_profileから読み込んでくれます。

$ sudo su - postgres
$ vi ~/.pgsql_profile
PATH=/usr/pgsql-13/bin:$PATH
MANPATH=/usr/pgsql-13/share/man:$MANPATH
PGDATA=/var/lib/pgsql/13/data
export PATH MANPATH PGDATA
$ source ~/.bash_profile

あとはデータベースの一覧を表示して接続確認をします。

$ psql -l
Password for user postgres:
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     |
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
(3 rows)