こんにちは、インフラエンジニアのやすです。
この記事では、Linux(CentOS7)のサーバに、PostgreSQLをインストールして初期化をする手順を紹介します。
CentOS7の標準リポジトリからインストールできるPostgreSQLのバージョンは「9.2」です。少し古いのですが、他のバージョンも大きく操作方法は変わらないですが、他のバージョンをインストールしたい場合は、パッケージをダウンロードしてインストールする必要があります。
PostgreSQL 9.2のインストール
事前確認
(1)インストールされていないことを確認
[root@localhost ~]# rpm -qa | grep postgre
[root@localhost ~]#
(2)postgreのデフォルトディレクトリがないことを確認
[root@localhost ~]# ls -l /var/lib | grep pgsql
[root@localhost ~]#
(3)postgresユーザが存在しないことを確認
[root@localhost ~]# cat /etc/passwd | grep postgres
[root@localhost ~]#
※なにか表示されたら「yum -y remove postgresql*」でアンインストール。
インストール
(4)インストール
[root@localhost ~]# yum -y install postgresql*
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp.riken.jp
* extras: ftp.riken.jp
* updates: ftp.riken.jp
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ postgresql.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-contrib.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-devel.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-docs.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-jdbc.noarch 0:9.2.1002-8.el7_8 を インストール
---> パッケージ postgresql-jdbc-javadoc.noarch 0:9.2.1002-8.el7_8 を インストール
---> パッケージ postgresql-libs.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-odbc.x86_64 0:09.03.0100-2.el7 を インストール
---> パッケージ postgresql-plperl.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-plpython.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-pltcl.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-server.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-static.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-test.x86_64 0:9.2.24-9.el7_9 を インストール
---> パッケージ postgresql-upgrade.x86_64 0:9.2.24-9.el7_9 を インストール
--> 依存性解決を終了しました。
依存性を解決しました
===================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
===================================================================================================================================================
インストール中:
postgresql x86_64 9.2.24-9.el7_9 updates 3.0 M
postgresql-contrib x86_64 9.2.24-9.el7_9 updates 553 k
postgresql-devel x86_64 9.2.24-9.el7_9 updates 962 k
postgresql-docs x86_64 9.2.24-9.el7_9 updates 6.9 M
postgresql-jdbc noarch 9.2.1002-8.el7_8 updates 453 k
postgresql-jdbc-javadoc noarch 9.2.1002-8.el7_8 updates 88 k
postgresql-libs x86_64 9.2.24-9.el7_9 updates 235 k
postgresql-odbc x86_64 09.03.0100-2.el7 base 273 k
postgresql-plperl x86_64 9.2.24-9.el7_9 updates 84 k
postgresql-plpython x86_64 9.2.24-9.el7_9 updates 97 k
postgresql-pltcl x86_64 9.2.24-9.el7_9 updates 60 k
postgresql-server x86_64 9.2.24-9.el7_9 updates 3.8 M
postgresql-static x86_64 9.2.24-9.el7_9 updates 95 k
postgresql-test x86_64 9.2.24-9.el7_9 updates 1.8 M
postgresql-upgrade x86_64 9.2.24-9.el7_9 updates 2.3 M
トランザクションの要約
===================================================================================================================================================
インストール 15 パッケージ
総ダウンロード容量: 21 M
インストール容量: 68 M
Downloading packages:
(1/15): postgresql-contrib-9.2.24-9.el7_9.x86_64.rpm | 553 kB 00:00:01
(2/15): postgresql-jdbc-javadoc-9.2.1002-8.el7_8.noarch.rpm | 88 kB 00:00:00
(3/15): postgresql-jdbc-9.2.1002-8.el7_8.noarch.rpm | 453 kB 00:00:02
(4/15): postgresql-libs-9.2.24-9.el7_9.x86_64.rpm | 235 kB 00:00:00
(5/15): postgresql-plperl-9.2.24-9.el7_9.x86_64.rpm | 84 kB 00:00:00
(6/15): postgresql-plpython-9.2.24-9.el7_9.x86_64.rpm | 97 kB 00:00:00
(7/15): postgresql-pltcl-9.2.24-9.el7_9.x86_64.rpm | 60 kB 00:00:00
(8/15): postgresql-devel-9.2.24-9.el7_9.x86_64.rpm | 962 kB 00:00:05
(9/15): postgresql-static-9.2.24-9.el7_9.x86_64.rpm | 95 kB 00:00:00
(10/15): postgresql-odbc-09.03.0100-2.el7.x86_64.rpm | 273 kB 00:00:02
(11/15): postgresql-test-9.2.24-9.el7_9.x86_64.rpm | 1.8 MB 00:00:03
(12/15): postgresql-server-9.2.24-9.el7_9.x86_64.rpm | 3.8 MB 00:00:06
(13/15): postgresql-9.2.24-9.el7_9.x86_64.rpm | 3.0 MB 00:00:10
(14/15): postgresql-upgrade-9.2.24-9.el7_9.x86_64.rpm | 2.3 MB 00:00:05
(15/15): postgresql-docs-9.2.24-9.el7_9.x86_64.rpm | 6.9 MB 00:00:11
---------------------------------------------------------------------------------------------------------------------------------------------------
合計 1.7 MB/s | 21 MB 00:00:12
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : postgresql-libs-9.2.24-9.el7_9.x86_64 1/15
インストール中 : postgresql-9.2.24-9.el7_9.x86_64 2/15
インストール中 : postgresql-server-9.2.24-9.el7_9.x86_64 3/15
インストール中 : postgresql-devel-9.2.24-9.el7_9.x86_64 4/15
インストール中 : postgresql-test-9.2.24-9.el7_9.x86_64 5/15
インストール中 : postgresql-static-9.2.24-9.el7_9.x86_64 6/15
インストール中 : postgresql-plperl-9.2.24-9.el7_9.x86_64 7/15
インストール中 : postgresql-pltcl-9.2.24-9.el7_9.x86_64 8/15
インストール中 : postgresql-upgrade-9.2.24-9.el7_9.x86_64 9/15
インストール中 : postgresql-plpython-9.2.24-9.el7_9.x86_64 10/15
インストール中 : postgresql-contrib-9.2.24-9.el7_9.x86_64 11/15
インストール中 : postgresql-docs-9.2.24-9.el7_9.x86_64 12/15
インストール中 : postgresql-odbc-09.03.0100-2.el7.x86_64 13/15
インストール中 : postgresql-jdbc-9.2.1002-8.el7_8.noarch 14/15
インストール中 : postgresql-jdbc-javadoc-9.2.1002-8.el7_8.noarch 15/15
検証中 : postgresql-9.2.24-9.el7_9.x86_64 1/15
検証中 : postgresql-plperl-9.2.24-9.el7_9.x86_64 2/15
検証中 : postgresql-pltcl-9.2.24-9.el7_9.x86_64 3/15
検証中 : postgresql-test-9.2.24-9.el7_9.x86_64 4/15
検証中 : postgresql-contrib-9.2.24-9.el7_9.x86_64 5/15
検証中 : postgresql-upgrade-9.2.24-9.el7_9.x86_64 6/15
検証中 : postgresql-odbc-09.03.0100-2.el7.x86_64 7/15
検証中 : postgresql-devel-9.2.24-9.el7_9.x86_64 8/15
検証中 : postgresql-libs-9.2.24-9.el7_9.x86_64 9/15
検証中 : postgresql-jdbc-javadoc-9.2.1002-8.el7_8.noarch 10/15
検証中 : postgresql-plpython-9.2.24-9.el7_9.x86_64 11/15
検証中 : postgresql-server-9.2.24-9.el7_9.x86_64 12/15
検証中 : postgresql-jdbc-9.2.1002-8.el7_8.noarch 13/15
検証中 : postgresql-static-9.2.24-9.el7_9.x86_64 14/15
検証中 : postgresql-docs-9.2.24-9.el7_9.x86_64 15/15
インストール:
postgresql.x86_64 0:9.2.24-9.el7_9 postgresql-contrib.x86_64 0:9.2.24-9.el7_9 postgresql-devel.x86_64 0:9.2.24-9.el7_9
postgresql-docs.x86_64 0:9.2.24-9.el7_9 postgresql-jdbc.noarch 0:9.2.1002-8.el7_8 postgresql-jdbc-javadoc.noarch 0:9.2.1002-8.el7_8
postgresql-libs.x86_64 0:9.2.24-9.el7_9 postgresql-odbc.x86_64 0:09.03.0100-2.el7 postgresql-plperl.x86_64 0:9.2.24-9.el7_9
postgresql-plpython.x86_64 0:9.2.24-9.el7_9 postgresql-pltcl.x86_64 0:9.2.24-9.el7_9 postgresql-server.x86_64 0:9.2.24-9.el7_9
postgresql-static.x86_64 0:9.2.24-9.el7_9 postgresql-test.x86_64 0:9.2.24-9.el7_9 postgresql-upgrade.x86_64 0:9.2.24-9.el7_9
完了しました!
[root@localhost ~]#
事後確認
(5)インストールされていることを確認
[root@localhost ~]# rpm -qa | grep postgres
postgresql-libs-9.2.24-9.el7_9.x86_64
postgresql-server-9.2.24-9.el7_9.x86_64
postgresql-test-9.2.24-9.el7_9.x86_64
postgresql-plperl-9.2.24-9.el7_9.x86_64
postgresql-upgrade-9.2.24-9.el7_9.x86_64
postgresql-contrib-9.2.24-9.el7_9.x86_64
postgresql-odbc-09.03.0100-2.el7.x86_64
postgresql-jdbc-javadoc-9.2.1002-8.el7_8.noarch
postgresql-9.2.24-9.el7_9.x86_64
postgresql-devel-9.2.24-9.el7_9.x86_64
postgresql-static-9.2.24-9.el7_9.x86_64
postgresql-pltcl-9.2.24-9.el7_9.x86_64
postgresql-plpython-9.2.24-9.el7_9.x86_64
postgresql-docs-9.2.24-9.el7_9.x86_64
postgresql-jdbc-9.2.1002-8.el7_8.noarch
[root@localhost ~]#
(6)postgresqlのデフォルトディレクトリがあることを確認
[root@localhost ~]# ls -l /var/lib | grep pgsql
drwx------. 4 postgres postgres 54 1月 7 09:13 pgsql
[root@localhost ~]#
(7)postgresユーザがいることを確認
[root@localhost ~]# cat /etc/passwd | grep postgres
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
[root@localhost ~]#
(8)postgresqlのバージョンを確認
[root@localhost ~]# psql --version
psql (PostgreSQL) 9.2.24
[root@localhost ~]#
(9)postgresql系のコマンドもできてますね。
[root@localhost ~]# ls -l /usr/bin | grep -e initdb -e pg_
-rwxr-xr-x. 1 root root 91224 12月 14 01:13 initdb
-rwxr-xr-x. 1 root root 16016 12月 14 01:13 pg_archivecleanup
-rwxr-xr-x. 1 root root 54672 12月 14 01:13 pg_basebackup
-rwxr-xr-x. 1 root root 28672 12月 14 01:13 pg_config
-rwxr-xr-x. 1 root root 24424 12月 14 01:13 pg_controldata
-rwxr-xr-x. 1 root root 37424 12月 14 01:13 pg_ctl
-rwxr-xr-x. 1 root root 313120 12月 14 01:13 pg_dump
-rwxr-xr-x. 1 root root 75664 12月 14 01:13 pg_dumpall
-rwxr-xr-x. 1 root root 37624 12月 14 01:13 pg_receivexlog
-rwxr-xr-x. 1 root root 32800 12月 14 01:13 pg_resetxlog
-rwxr-xr-x. 1 root root 129832 12月 14 01:13 pg_restore
-rwxr-xr-x. 1 root root 20240 12月 14 01:13 pg_standby
-rwxr-xr-x. 1 root root 20336 12月 14 01:13 pg_test_fsync
-rwxr-xr-x. 1 root root 16016 12月 14 01:13 pg_test_timing
-rwxr-xr-x. 1 root root 104704 12月 14 01:13 pg_upgrade
[root@localhost ~]#
PostgreSQLの初期化
インストールしただけだとPostgreSQLは起動できないので、次に初期化(PostgreSQLの設定ファイルやDBのデータが溜まるディレクトリを設定)します。
事前確認
(1)postgresユーザに切り替え
[root@localhost ~]# su - postgres
-bash-4.2$
-bash-4.2$ whoami
postgres
-bash-4.2$
(2)PostgreSQLの環境変数を確認
-bash-4.2$ env | grep PG
PGDATA=/var/lib/pgsql/data
-bash-4.2$
(3)データディレクトリになにもないことを確認(初期化されていないこと)
-bash-4.2$ ls -l /var/lib/pgsql/data
合計 0
-bash-4.2$
初期化
(4)初期化
-bash-4.2$ initdb --locale=C --encoding=UTF8
データベースシステム内のファイルの所有者は"postgres"ユーザでした。
このユーザがサーバプロセスを所有しなければなりません。データベースクラスタはロケール"C"で初期化されます。
デフォルトのテキスト検索設定はenglishに設定されました。ディレクトリ/var/lib/pgsql/dataの権限を設定しています ... ok
サブディレクトリを作成しています ... ok
デフォルトのmax_connectionsを選択しています ... 100
デフォルトの shared_buffers を選択しています ... 32MB
設定ファイルを作成しています ... ok
/var/lib/pgsql/data/base/1にtemplate1データベースを作成しています ... ok
pg_authidを初期化しています ... ok
依存関係を初期化しています ... ok
システムビューを作成しています ... ok
システムオブジェクトの定義をロードしています ... ok
照合順序を作成しています ... ok
変換を作成しています ... ok
ディレクトリを作成しています ... ok
組み込みオブジェクトに権限を設定しています ... ok
情報スキーマを作成しています ... ok
PL/pgSQL サーバサイド言語をロードしています ... ok
template1データベースをバキュームしています ... ok
template1からtemplate0へコピーしています ... ok
template1からpostgresへコピーしています ... ok
警告: ローカル接続向けに"trust"認証が有効です。
pg_hba.confを編集する、もしくは、次回initdbを実行する時に-Aオプショ
ン、または、--auth-localおよび--auth-hostを使用することで変更するこ
とができます。成功しました。以下を使用してデータベースサーバを起動することができます。
postmaster -D /var/lib/pgsql/data
または
pg_ctl -D /var/lib/pgsql/data -l logfile start
-bash-4.2$
事後確認
(5)データディレクトリにファイルがあることを確認(初期化されたこと)
-bash-4.2$ ls -l /var/lib/pgsql/data
合計 40
-rw-------. 1 postgres postgres 4 1月 7 11:13 PG_VERSION
drwx------. 5 postgres postgres 41 1月 7 11:13 base
drwx------. 2 postgres postgres 4096 1月 7 11:13 global
drwx------. 2 postgres postgres 18 1月 7 11:13 pg_clog
-rw-------. 1 postgres postgres 4476 1月 7 11:13 pg_hba.conf
-rw-------. 1 postgres postgres 1636 1月 7 11:13 pg_ident.conf
drwx------. 4 postgres postgres 36 1月 7 11:13 pg_multixact
drwx------. 2 postgres postgres 18 1月 7 11:13 pg_notify
drwx------. 2 postgres postgres 6 1月 7 11:13 pg_serial
drwx------. 2 postgres postgres 6 1月 7 11:13 pg_snapshots
drwx------. 2 postgres postgres 6 1月 7 11:13 pg_stat_tmp
drwx------. 2 postgres postgres 18 1月 7 11:13 pg_subtrans
drwx------. 2 postgres postgres 6 1月 7 11:13 pg_tblspc
drwx------. 2 postgres postgres 6 1月 7 11:13 pg_twophase
drwx------. 3 postgres postgres 60 1月 7 11:13 pg_xlog
-rw-------. 1 postgres postgres 19809 1月 7 11:13 postgresql.conf
-bash-4.2$
作り直したい時は、/var/lib/pgsql/data
配下のファイルを全て削除して、またinitdbを実行すればOKです。
まとめ
今回は、PostgreSQLのインストールと初期化の手順を紹介しました。
作って壊して色々試してみましょうー!
ではまた、バイバーイ♪