
updated on 2019-06-11
どうやら amazon-linux-extras コマンドでは psql コマンドがインストールされるだけのようです。
なので、yumで入れましょう
前提: EC2仮想サーバーにsshできる状態、ec2内にユーザーを作成済み(筆者の場合tatsuya)
と言うことで、postgresのサーバは普通に yum install でインストールしましょう。
[tatsuya@ip-10-0-0-238 ~]$sudoyuminstall-y postgresql postgresql-server postgresql-devel postgresql-contrib postgresql-docs
結果(一部)
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core
〜〜中略〜〜
Complete!
確認
$ psql --version
psql (PostgreSQL) 9.2.4
次に初期設定を行います。以下のコマンドでできます。
## 初期化(PostgreSQLインストール後には必ず実施)
[tatsuya@ip-10-0-0-238 ~]$sudo/sbin/servicepostgresql initdb
## 起動・停止・再起動[tatsuya@ip-10-0-0-238 ~]$sudo/sbin/servicepostgresql startStarting postgresql service:[ OK ][tatsuya@ip-10-0-0-238 ~]$$sudo/sbin/servicepostgresql stopStopping postgresql service: [ OK ][tatsuya@ip-10-0-0-238 ~]$sudo/sbin/servicepostgresql restartStopping postgresql service: [ OK ]Starting postgresql service:
必要に応じて以下の自動起動設定を行なってください。
$sudo/sbin/chkconfig--list |greppostgresqlpostgresql 0:off 1:off 2:off 3:off 4:off 5:off 6:off$sudo/sbin/chkconfigpostgresql on$sudo/sbin/chkconfig--list |greppostgresqlpostgresql0:off 1:off 2:on 3:on 4:on 5:on 6:off
ec2-userにてPostgreSQL用ユーザーpostgresのパスワードを変更します。
[tatsuya@ip-10-0-0-238 ~]$sudopasswdpostgresユーザー postgres のパスワードを変更。新しいパスワード:(**************)新しいパスワードを再入力してください:(**************)passwd: 全ての認証トークンが正しく更新できました。
ユーザが変更出来ることを確認してみます。
[tatsuya@ip-10-0-0-238 ~]$su- postgresパスワード:-bash-4.1$exit-bash-4.1
次にpsqlコマンドで接続します。
psql -U postgres -h localhost
以下のような結果が得られればOKです。
psql (9.6.8)
Type "help" for help.
postgres=#設定ファイルpostgresql.confを編集。listen_addressは行先頭のコメントを除去し内容を『*』に変更
$su- postgres
$vi/var/lib/pgsql/data/postgresql.conf
-----
...
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses ='*'# what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for allport = 5432# (change requires restart)
次いでpg_hba.confの編集。ここは以下の様に最低限の設定でひとまず進めます。
$vi/var/lib/pgsql/data/pg_hba.conf# local all all peer # 以下に変更(peer認証を取り除く) local all all md5 # host all all 127.0.0.1/32 ident # 以下に変更(localhostからの時passwordを求めない) host all all 127.0.0.1/32 trust
trust でローカル環境なら psql -U postgres -h localhost でパスワードなしでもアクセスできるようになる
postgresユーザとは別に、今回の動作確認用のDB接続ユーザーを作成します。まずは、実行ユーザをpostgresにチェンジ。
$su- postgresパスワード: (***********)-bash-4.1$
| ユーザー名 | tatsuya |
| パスワード | cmtestpass |
| データベース(テスト環境) | db_test |
| データベース(開発環境) | db_development |
| データベース(本番環境) | db_production |
ユーザー名はec2-user以外の登録したユーザを作成する。理由はEC2のユーザー名と、これから作成するpostgresqlサーバー内のユーザー名を合わせることで、勝手にユーザーを識別してくれる
[tatsuya@ip-10-0-0-238 ~]$ psql -l -U tatsuya-Uを指定しなくてもpsql -lだけでユーザー名tatsuyaとして認識してくれてちょっと楽
-Uオプション指定の手間なんて気にしないという人はcmtestuserなり好きなユーザー名を作成してください
今回作業用の接続ユーザー及びパスワードの設定は上記内容とします。まずはDB接続ユーザー作成/パスワード設定
-bash-4.1$ createuser -AdPE tatsuya Enter password for new role: (作成ユーザー名に対応するパスワードを設定) Enter it again: (上記内容を再入力) -bash-4.1$
ユーザーが利用するDBの作成(パスワードは上記作成ユーザのパスワードを入力)
$ createdb -U tatsuya -W db_test $ createdb -U tatsuya -W db_production $ createdb -U tatsuya -W db_development Password: (**********) -bash-4.1$ /usr/bin/pg_ctl restart -D /var/lib/pgsql/data -o "-p 5432" -w -t 300 # または [tatsuya@ip-10-0-0-238 ~]$ sudo /sbin/service postgresql restart
以上でDBに関する接続ユーザー情報の設定は完了。
$ su - postgres パスワード: (***********) -bash-4.1$ psql psql (9.2.24) Type "help" for help. postgres=# CREATE ROLE ユーザ名 LOGIN CREATEDB PASSWORD 'ここにパスワード'; # ユーザ名をtatsuya,パスワードにはpgサーバーにユーザー名でログインするときのパスワードを指定 postgres=# CREATE DATABASE データベース名 OWNER ユーザ名; # db_test, db_development, db_productionを作成
ちなみにユーザー(ROLE)のパスワードを変更したい、忘れたので上書きしたい時とか以下でできる
postgres=# ALTER ROLE postgres with password 'postgres';
上記サンプルでは「postgres」という文字列のパスワードを設定しています
$su- tatsuyaパスワード:
[tatsuya@ip-10-0-0-238 ~]$ psql -l パスワード: データベース一覧 名前 | 所有者 | エンコーディング | 照合順序 | Ctype(変演算子) | アクセス権 -------------------------+----------+------------------+-------------+---------- ---------+----------------------- db_test | tatsuya | UTF8 | en_US.UTF-8 | en_US.UTF-8 | db_production | tatsuya | UTF8 | en_US.UTF-8 | en_US.UTF-8 | db_development | tatsuya | UTF8 | en_US.UTF-8 | en_US.UTF-8 | postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | 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 (6 行)
参考記事 https://dev.classmethod.jp/cloud/aws/install-postgresql-on-aws-ec2/
https://www.setouchino.cloud/blogs/95
http://cocodrips.hateblo.jp/entry/2017/09/04/213323
アンインストール
sudo yum -y remove postgresql*
sudo rm -rf /var/lib/pgsql*
sudo rm -rf /var/lib/postgresql*