Debian Lennyのpsqlに現在のログインユーザでアクセス出来るようにする | labunixのラボゆにっくす

labunixのラボゆにっくす

Debian [ Lenny | squueze | kfreebsd ] amd64


■現在ログインしているユーザからpsqlをpostgresユーザとして起動する

$ su postgres -c 'psql'

■現在のユーザから直接だとダメ。

$ psql postgres -W
Password for user postgres:
psql: FATAL: Ident authentication failed for user "postgres"

■管理者アカウントはpostgresで良いのだが、
 その他すべてのユーザが「ident sameuser」になっているので、
 ログインシェル=psqlのログインユーザである必要がある

$ sudo grep -B 1 ident /etc/postgresql/8.3/main/pg_hba.conf
# METHOD can be "trust", "reject", "md5", "crypt", "password", "gss", "sspi",
# "krb5", "ident", "pam" or "ldap". Note that "password" sends passwords
--
#
# OPTION is the ident map or the name of the PAM service, depending on METHOD.
--
# Database administrative login by UNIX sockets
local all postgres ident sameuser
--
# "local" is for Unix domain socket connections only
local all all ident sameuser

■現在のログインユーザを追加する(スーパーユーザとして)

$ su postgres -c 'createuser '"`whoami`"
パスワード:
Shall the new role be a superuser? (y/n) y

■現在のログインユーザを削除する

$ su postgres -c 'dropuser '"`whoami`"
パスワード:

■パスワード無しで入れてしまうので、パスワードを設定

 alter user username password 'password'

 例えば1ユーザの場合
 ※履歴やファイルのアクセス権の管理等は設定出来るものとして。

$ password=password
$ echo 'alter user '`whoami`' password '\'"${password}"\' > alter_user_passwd.sql
$ psql -d postgres -f alter_passwd.sql
ALTER ROLE

■パスワードログインできるか確認する

$ psql -U `whoami` -d postgres -W

■ログインユーザ名とすべてのユーザのユーザ名とパスワードの有無を確認する
 ※「********」が無いユーザはパスワードの設定がされていない。

$ cat check_user.sql
select * from user;
select usename,passwd from pg_user;

$ psql -d postgres -f check_user.sql
current_user
--------------
labunix
(1 row)

usename | passwd
----------+----------
postgres | ********
labunix | ********
(2 rows)