インストール -> 12.1
PostgreSQL: 12.1
CentOS: 7.5.1804
- ソースコードビルド
- CLI
- CentOS 標準の Yum リポジトリ
- PostgreSQL 開発元の Yum リポジトリ
- GUI
と色々な方法があるみたいだけど
今回は参考サイトに倣って
PostgreSQL 開発元の Yum リポジトリからインストールすることにした。
yum list installed | grep postgresql
# サービス停止
systemctl stop postgresql-11.service
# 確認
ps aux | grep postgres
# アンインストール
yum remove postgresql*
公式:Accessing to the PostgreSQL RPM Repositories
こちらの「CentOS 7 – x86_64」のリンクアドレスをコピーする。
https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
これを引数として、リポジトリパッケージをインストール。
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
__snip__
インストール:
pgdg-redhat-repo.noarch 0:42.0-5
完了しました!
yum info postgresql12-server
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
* base: ftp-srv2.kddilabs.jp
* epel: www.ftp.ne.jp
* extras: ftp-srv2.kddilabs.jp
* remi-safe: ftp.riken.jp
* updates: ftp-srv2.kddilabs.jp
利用可能なパッケージ
名前 : postgresql12-server
アーキテクチャー : x86_64
バージョン : 12.1
リリース : 1PGDG.rhel7
容量 : 4.9 M
リポジトリー : pgdg12/7/x86_64
要約 : The programs needed to create and run a PostgreSQL server
URL : https://www.postgresql.org/
ライセンス : PostgreSQL
説明 : PostgreSQL is an advanced Object-Relational database management system (DBMS).
: The postgresql12-server package contains the programs needed to create
: and run a PostgreSQL server, which will in turn allow you to create
: and maintain PostgreSQL databases.
yum install -y postgresql12-server
__snip__
インストール:
postgresql12-server.x86_64 0:12.1-1PGDG.rhel7
依存性関連をインストールしました:
postgresql12.x86_64 0:12.1-1PGDG.rhel7 # これが client らしい
postgresql12-libs.x86_64 0:12.1-1PGDG.rhel7
完了しました!
ん~、参考サイトでは、python3 が依存性関連としてインストールされていたけど
私の場合は出てないなぁ。
ll /usr/pgsql-12/
合計 12
drwxr-xr-x 2 root root 4096 11月 17 18:36 bin
drwxr-xr-x 3 root root 4096 11月 17 18:36 lib
drwxr-xr-x 7 root root 4096 11月 17 18:36 share
psql --version
psql (PostgreSQL) 12.1
PostgreSQL の postgres ユーザは、実は2人いる。
一人目は、インストール時に自動で作成される、OS のユーザ。
二人目は、PostgreSQL のスーパーユーザ。
su - postgres
最終ログイン: 2019/11/17 (日) 19:40:30 JST日時 pts/0
これが、OS ユーザ。
id postgres
uid=26(postgres) gid=26(postgres) groups=26(postgres)
tail /etc/passwd
__snip__
postgres:x:26:26:PostgreSQL Server:/var/lib/pgsql:/bin/bash
そして、
PostgreSQLをインストールした時点で、
自動的にデータベースの管理ユーザーであるpostgresユーザーが作成されます。
ただ、このユーザーのパスワードは未設定の状態であるため、アカウントロック状態となっており、
このままではrootユーザーからsuコマンドでスイッチする方法以外でのログインができません。PostgreSQL で始める DB 入門:ユーザーとパスワードの設定
とのこと。
なので、OS の postgres ユーザでログインする必要がある場合は、
下記コマンドにて、パスワードを再設定する必要がある。
passwd postgres
ユーザー postgres のパスワードを変更。
新しいパスワード:
新しいパスワードを再入力してください:
\du
ロール一覧
ロール名 | 属性 | 所属グループ
----------+--------------------------------------------------------------------------+--------------
postgres | スーパユーザ, ロール作成可, DB作成可, レプリケーション可, RLS のバイパス | {}
これが、PostgreSQL スーパーユーザ。
そして、下記の SQL にて、progres ユーザの情報を見ると、
設定した覚えのないパスワードが。。。
SELECT * FROM pg_user;
usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
postgres | 10 | t | t | t | t | ******** | |
なので、パスワードを再設定する。
ALTER ROLE postgres WITH PASSWORD 'password';
ALTER ROLE
さらに、名前が postgres のデータベースまで存在するので、非常にややこしい。
\l
データベース一覧
名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変換演算子) | アクセス権限
-----------+----------+------------------+----------+-------------------+-----------------------
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
PostgreSQL で始める DB 入門:ユーザーとパスワードの設定
ローカル環境は、Windows10 で動かしている VirtualBox 内の CentOS へインストールしたので
外部からアクセスできるように設定する必要がある。
vim /var/lib/pgsql/12/data/postgresql.conf
- #listen_addresses = 'localhost' # what IP address(es) to listen on;
+ listen_addresses = '*' # what IP address(es) to listen on;
vim /var/lib/pgsql/12/data/pg_hba.conf
- host all all 127.0.0.1/32 ident
+ host all all all trust
Method trust は、パスワード無しで認証される。
これでようやく phpPgAdmin からログインできたけど
Method を ident にすると、パスワードを入力しても、ログインできない。
難しい。 🙄
また、今回は必要なかったけど
場合によっては、VirtualBox 側の設定も必要になるかも。
Qiita:【初心者向け】サーバに構築したPostgreSQLをWindowsからツールで使えるようになるまでを一から説明します。(2018-12-03)
→他の記事で、pg_hba.conf に IP アドレスを指定する方法が載っていたけど、それではだめだった。こちらで all を設定できることを知り、なんとか解決
The Life:ホストOSから VIRTUALBOX の中の POSTGRESQL にアクセスする方法(2018-01-01)
→VirtualBox のポートフォワーディング設定についての記事あり
データベースクラスタという文言は、PostgreSQL 独自のものだそうな。
impress:データベース・クラスタの概要(2010-10-05)
に拠れば
- 高可用性(Transactional)
- 並列処理(Analytic)
- 性能向上(Online)
これらを担保するためのシステムとのこと。
PGSETUP_INITDB_OPTIONS="-E UTF8 --locale=C" /usr/pgsql-12/bin/postgresql-12-setup initdb
Initializing database ... OK
最近は、寿司ビール問題から、MySQL では、utf8mb4 とするようにしているけど
PostgreSQL では、UTF8 で問題ないらしい。
–locale=C
は、「ロケールなし」。
–no-locale
と同義とのこと。
ロケールは言語や国によって異なる文字の扱いや並び替え順を指定するものです。
日本語ではロケールを使う必要があまりなく、ロケールを使うと文字の処理が遅くなり、
LIKE で通常のインデックスも使えなくなるので、ロケールを使わないようにします。Qiita:PostgreSQL を CentOS にインストールするには(2019-11-06)
ロケールが決まると、必ずそれに一致するエンコーディングを使わなければならないことを示しています。
$ initdb –encoding=UTF-8 –locale=ja_JP.UTF-8
~~~~~ ~~~~~Let’s Postgres:ロケール(国際化と地域化)(2009-06-18)
ll /var/lib/pgsql/12/data
合計 52
-rw------- 1 postgres postgres 3 11月 17 19:08 PG_VERSION
drwx------ 5 postgres postgres 41 11月 17 19:08 base
drwx------ 2 postgres postgres 4096 11月 17 19:08 global
drwx------ 2 postgres postgres 6 11月 17 19:08 log
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_commit_ts
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_dynshmem
-rw------- 1 postgres postgres 4269 11月 17 19:08 pg_hba.conf
-rw------- 1 postgres postgres 1636 11月 17 19:08 pg_ident.conf
drwx------ 4 postgres postgres 68 11月 17 19:08 pg_logical
drwx------ 4 postgres postgres 36 11月 17 19:08 pg_multixact
drwx------ 2 postgres postgres 18 11月 17 19:08 pg_notify
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_replslot
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_serial
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_snapshots
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_stat
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_stat_tmp
drwx------ 2 postgres postgres 18 11月 17 19:08 pg_subtrans
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_tblspc
drwx------ 2 postgres postgres 6 11月 17 19:08 pg_twophase
drwx------ 3 postgres postgres 60 11月 17 19:08 pg_wal
drwx------ 2 postgres postgres 18 11月 17 19:08 pg_xact
-rw------- 1 postgres postgres 88 11月 17 19:08 postgresql.auto.conf
-rw------- 1 postgres postgres 26586 11月 17 19:08 postgresql.conf
いつも通り。
# 起動
systemctl start postgresql-12.service
# 停止
systemctl stop postgresql-12.service
# 再起動
systemctl restart postgresql-12.service
# 確認
systemctl status postgresql-12.service
# 自動起動
systemctl enable postgresql-12.service
スーパーユーザ postgres の環境変数設定を行う。
su - postgres
最終ログイン: 2019/11/17 (日) 19:40:30 JST日時 pts/0
これで、postgres ユーザとして、コマンドを操作する。
vim ~/.pgsql_profile
PATH=/usr/pgsql-12/bin:$PATH
MANPATH=/usr/pgsql-12/share/man:$MANPATH
PGDATA=/var/lib/pgsql/12/data
export PATH MANPATH PGDATA
. ~/.bash_profile
psql -l
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)
ん~、環境変数設定をする前から、これは見られたけど
どういうことなんだろう。
Qiita:PostgreSQL を CentOS にインストールするには(2019-11-06)
Qiita:CentOS 7.5にPostgreSQL12をインストールする(2019-10-29)
a23note:PostgreSQL12 yum install CentOS7 (2)(2019-10-15)
Saba note:PostgreSQL インストールと初期設定(2019-10-11)
→旧バージョン対応を参考にさせていただいた
これ、作ったはいいけど、どこにあるの?
ll /home
合計 0
/var/lib/pgsql/
合計 0
drwx------ 4 postgres postgres 51 11月 17 19:08 12
find ./ -name '*pgsql_profile*'
find ./ -name '*pgsql_profile'
find ./ -name '.pgsql_profile'
全て反応なし。
データベースクラスタを覗いた際に
下記のような警告が出た。
これは、ユーザ変更をする際に
su postgres
このように -(ハイフン)を付けなかったために起きた。
ハイフンなしだと、不完全な切り替えらしい。