3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[PostgreSQL]ユーザーパスワードを忘れてしまったときの対処方法

Posted at

Postgres初心者ですpsql -U postgresでログインしようとしたところパスワードが合致せず詰まったので、備忘録としてメモ、下記記事を参考にさせていただきました。

解決方法

pg_hba.confファイルを探します。

$ find / -print |grep pg_hba.conf
-> /var/lib/pgsql/data/pg_hba.conf

vimを使ってファイルを編集しましょう

$ vim /var/lib/pgsql/data/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            password
# IPv6 local connections:
host    all             all             ::1/128                 password

これを下記のようにMETHODの部分をTrust変更します。

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust

postgresを再起動します。

$ service postgresql restart

するとログインできるようになっています

psql -U postgres
postgres=#

パスワードを変更する

このままではパスワードが設定されていなくセキュアではないので、postgreユーザのパスワードを変更してpg_hba.confファイルを元に戻しましょう

postgres=# ALTER ROLE postgres WITH PASSWORD {'パスワード'};
ALTER ROLE

そしたらpg_hba.confファイルの設定を戻します。posgresからログアウトして再びvimを使います。

$ vim /var/lib/pgsql/data/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            password
# IPv6 local connections:
host    all             all             ::1/128                 password

元に戻して一件落着!
これでログイン時にパスワードが聞かれるようになります。

3
0
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
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?