LoginSignup
1
1

More than 1 year has passed since last update.

【初心者】【備忘】EC2にposgresql12.3をインストール

Last updated at Posted at 2021-10-04

EC2にpostgresql12.3インストール

repoインストール

EC2のディストリビューション?、バージョンを確認する

 # cat /etc/system-release
 Amazon Linux release 2 (Karoo)

Amazon Linux はRHEL7ベースらしいのでここからのrepoファイルをインストールする

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

repoファイルのダウンロード。
EC2のrootユーザで作業

# wget --no-check-certificate https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

repoファイルのインストール
/etc/redhat-releaseファイルへの依存関係を無視してインストールするため--nodepsオプションを付けてインストール

# rpm -Uvh --nodeps ./pgdg-redhat-repo-latest.noarch.rpm

repoの確認

# yum repolist
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
27 packages excluded due to repository priority protections
repo id                      repo name                                           status
!amzn2-core/2/x86_64         Amazon Linux 2 core repository                       26,253
amzn2extra-docker/2/x86_64   Amazon Extras repo for docker                            46
pgdg-common/x86_64           PostgreSQL common RPMs for RHEL/CentOS 7 - x86_64     320+6
pgdg10/x86_64                PostgreSQL 10 for RHEL/CentOS 7 - x86_64              972+7
pgdg11/x86_64                PostgreSQL 11 for RHEL/CentOS 7 - x86_64            1,075+4
pgdg12/x86_64                PostgreSQL 12 for RHEL/CentOS 7 - x86_64              657+1
pgdg13/x86_64                PostgreSQL 13 for RHEL/CentOS 7 - x86_64              390+1
pgdg14/x86_64                PostgreSQL 14 for RHEL/CentOS 7 - x86_64                120
pgdg96/x86_64                PostgreSQL 9.6 for RHEL/CentOS 7 - x86_64             933+8
treasuredata/x86_64          TreasureData                                              5
repolist: 30,771

repo編集

このままだとyum installが上手くいかないので、yum.repos.dのファイルを編集する
$releaseverという変数にRHELやCentOSのバージョン数(7とか8)が入るらしいけど、EC2のAmazon Linuxだとsystem-releaseという文字列が入ってyum installが上手くいかない。
参考:【CentOS7】yum の $releasever、$basearch と $infra を知る方法
置換する7はRHEL/CentOSのバージョン7という意味かな。7向けのバイナリを使えというパスになる。

# sed --in-place -e "s/\$releasever/7/g" /etc/yum.repos.d/pgdg-redhat-all.repo

リポジトリにバージョン12.3が入ってるか確認

#yum --showduplicates search postgresql12-server
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
27 packages excluded due to repository priority protections
=========================== N/S matched: postgresql12-server ===========================
postgresql12-server-12.1-2PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.2-1PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.2-2PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.3-1PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.3-5PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.3-5PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.4-1PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.5-1PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.6-1PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.7-1PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server
postgresql12-server-12.8-1PGDG.rhel7.x86_64 : The programs needed to create and run a
                                            : PostgreSQL server

postgresインストール

postgresql12-server-12.3をインストール

# yum install postgresql12-server-12.3

めんどくさい。AWSではRDS使えばいいじゃんってことかな。

postgresのセットアップ

初期化

# /usr/pgsql-12/bin/postgresql-12-setup initdb

postgresの起動設定

systemdに登録されているか確認

# systemctl list-unit-files |grep postgres
postgresql-12.service                         disabled

ユニットファイルはあるけど、自動起動設定はdisabledになっている。
自動起動するようにする

# systemctl enable postgresql-12.service 
Created symlink from /etc/systemd/system/multi-user.target.wants/postgresql-12.service to /usr/lib/systemd/system/postgresql-12.service.

enabledに変わった

# systemctl list-unit-files |grep postgres
postgresql-12.service                         enabled 

自動起動スクリプトの確認
/etc/systemd/system/multi-user.target.wants/にpostgresのスクリプトのリンクが作成されてる

# ll /etc/systemd/system/multi-user.target.wants/ | grep postgres
lrwxrwxrwx 1 root root 45 Oct  4 20:45 postgresql-12.service -> /usr/lib/systemd/system/postgresql-12.service

postgresの起動

ステータスを確認してみる

# systemctl status postgresql-12.service 
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: https://www.postgresql.org/docs/12/static/

起動してみる

# systemctl start postgresql-12.service 

ステータスを確認してみる

# systemctl status postgresql-12.service
● postgresql-12.service - PostgreSQL 12 database server
   Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
   Active: active (running) since Mon 2021-10-04 20:47:53 JST; 18s ago
     Docs: https://www.postgresql.org/docs/12/static/
  Process: 2245 ExecStartPre=/usr/pgsql-12/bin/postgresql-12-check-db-dir ${PGDATA} (code=exited, status=0/SUCCESS)
 Main PID: 2252 (postmaster)
   CGroup: /system.slice/postgresql-12.service
           ├─2252 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
           ├─2255 postgres: logger   
           ├─2257 postgres: checkpointer   
           ├─2258 postgres: background writer   
           ├─2259 postgres: walwriter   
           ├─2260 postgres: autovacuum launcher   
           ├─2261 postgres: stats collector   
           └─2262 postgres: logical replication launcher   

Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal systemd[1]: Starting P...
Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal postmaster[2252]: 2021...
Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal postmaster[2252]: 2021...
Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal postmaster[2252]: 2021...
Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal postmaster[2252]: 2021...
Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal postmaster[2252]: 2021...
Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal postmaster[2252]: 2021...
Oct 04 20:47:53 ip-172-31-30-1.ap-northeast-1.compute.internal systemd[1]: Started Po...
Hint: Some lines were ellipsized, use -l to show in full.

postgresqlクライアントから接続してみる

postgresユーザからpsqlコマンドで接続

# su - postgres
-bash-4.2$ psql
psql (12.3)
Type "help" for help.

postgres=# 

繋がった!!
\qでクライアントを抜けられます。

他サーバなどからの接続設定

  • postgresユーザのパスワード設定 デフォルトの/var/lib/pgsql/12/data/pg_hba.conflocalからの接続はpeer認証になっているので、OSのpostgresユーザから接続する
pg_hba.conf
local   all             all                                     peer
# su - postgres
Last login: Tue Oct  5 19:35:49 JST 2021 on pts/0
-bash-4.2$ psql
psql (12.3)
Type "help" for help.

postgres=# 
postgres=# ALTER USER postgres with encrypted password 'xxxxxxxx';
  • md5認証に変更
pg_hba.conf
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     md5
host    replication     all             127.0.0.1/32            md5
host    replication     all             ::1/128                 md5
  • 接続設定も変更/var/lib/pgsql/12/data/postgresql.conf
postgresql.conf
listen_addresses = '*'
  • 設定反映
# systemctl reload postgresql-12.service 
  • 接続確認
# psql -U postgres -W
Password: 
psql (12.3)
Type "help" for help.

postgres=# 
1
1
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
1
1