今回の環境
- CentOS 6.6 64bit
- postgresql-8.4.20-1.el6_5.x86_64 ← これを今回入れます。
導入
# yum -y install postgresql postgresql-server # service postgresql initdb # service postgresql start
認証設定
# vi /var/lib/pgsql/data/pg_hba.conf local all all ident ←ここを適宜変える
- ident … OSのユーザ名とPostgresSQLのユーザ名が一致していたら受け入れる。デフォルトっぽい。
- md5 … パスワード認証。
- trust … 無条件に受け入れる。
↓例えばこんな感じ。postgresユーザのみ、ユーザ名による認証。それ以外はパスワード認証。
# vi /var/lib/pgsql/data/pg_hba.conf local all postgres ident local all all md5
ユーザ作成/削除
ユーザ作成。
[postgres]$ createuser myuser -P Enter password for new role: xxxxxx Enter it again: xxxxxx Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) y Shall the new role be allowed to create more new roles? (y/n) n
ユーザ削除。
[postgres]$ dropuser myuser
データベース作成/削除
データベース作成。
[postgres]$ createdb mydatabase
データベース削除。
[postgres]$ dropdb mydatabase
データベースに接続
$ psql mydatabase myuser Password for user myuser: xxxxxx … mydatabase=>
適当に操作してみる
SQL> CREATE TABLE hoge(a INT); SQL> INSERT INTO hoge(a) VALUES(10); SQL> INSERT INTO hoge(a) VALUES(20); SQL> SELECT * FROM hoge; a ---- 10 20 (2 rows)
トラブルシューティング
psql: FATAL: Ident authentication failed for user "~~"
$ psql myuser myuser psql: FATAL: Ident authentication failed for user "myuser"
ユーザ名による認証を試みたが、ユーザ名が一致しなかった。
認証方式 (pg_hba.conf により設定) が ident になっている場合、OSのユーザ名とPostgreSQLのユーザ名を比較することにより認証を行います。たとえば「psql myuser myuser」を通すためにはOSのユーザもmyuserである必要があります。
もう意味わかんなくなったら認証方式を trust にしちゃうと楽ですよ。
パスワード設定したのにパスワード聞かれずに認証通っちゃう
認証方式 (pg_hba.conf により設定) が ident とか trust だと、ユーザにパスワード設定してあってもパスワード入力無しでログインできちゃうみたいです。パスワード入力促したいなら md5 あたりに設定しておくと良いです。
ログアウトできない
ログアウトのコマンドは「\q」です。
おしまい
ヽ(◔౪◔)ノ