実現したいこと
- Ubuntu22.04にPostgreSQLをインストール
- データベース
db_test
とロールrole_test
を作成し、db_test
の所有者をrole_test
に変更 - 作成したデータベースとユーザーでPostgreSQLに接続
bash
1sudo apt install postgresql postgresql-contrib libpq-dev 2 3sudo -u postgres psql 4create database db_test; 5create role role_test with login password '1234'; 6alter database db_test owner to role_test; 7exit 8 9psql -h localhost -p 5432 -U role_test -d db_test
発生している問題・分からないこと
- WSL2上のUbuntu22.04では、上記手順 3. で認証エラーが発生して接続できない。
- EC2上のUbuntu22.04では、同じ手順で問題なく接続可能。
WSL2の場合
bash
1ubuntu@DESKTOP-xxxxxxx:~$ psql -h localhost -p 5432 -U role_test -d db_test 2Password for user role_test: 3# (パスワードを入力) 4psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "role_test" 5connection to server at "localhost" (127.0.0.1), port 5432 failed: FATAL: password authentication failed for user "role_test"
EC2の場合
bash
1ubuntu@ip-x-x-x-x:~$ psql -h localhost -p 5432 -U role_test -d db_test 2Password for user role_test: 3# (パスワードを入力) 4psql (14.10 (Ubuntu 14.10-0ubuntu0.22.04.1)) 5SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off) 6Type "help" for help. 7 8db_test=>
エラーメッセージ
error
1上記参照______
該当のソースコード
特になし
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
WSL2もEC2も、以下は同一でした。
- Ubuntuのバージョン
- PostgreSQLのバージョン
pg_hba.conf
の内容
WSL2の場合
bash
1ubuntu@DESKTOP-xxxxxxx:~/Project$ cat /etc/os-release 2PRETTY_NAME="Ubuntu 22.04.3 LTS" 3NAME="Ubuntu" 4VERSION_ID="22.04" 5VERSION="22.04.3 LTS (Jammy Jellyfish)" 6VERSION_CODENAME=jammy 7ID=ubuntu 8ID_LIKE=debian 9HOME_URL="https://www.ubuntu.com/" 10SUPPORT_URL="https://help.ubuntu.com/" 11BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" 12PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" 13UBUNTU_CODENAME=jammy 14 15# ========================================= 16psql -V 17psql (PostgreSQL) 14.10 (Ubuntu 14.10-0ubuntu0.22.04.1) 18 19sudo nano /etc/postgresql/14/main/pg_hba.conf 20 21# ... 22local all postgres peer 23 24# TYPE DATABASE USER ADDRESS METHOD 25 26# "local" is for Unix domain socket connections only 27local all all peer 28# IPv4 local connections: 29host all all 127.0.0.1/32 scram-sha-256 30# IPv6 local connections: 31host all all ::1/128 scram-sha-256 32# Allow replication connections from localhost, by a user with the 33# replication privilege. 34local replication all peer 35host replication all 127.0.0.1/32 scram-sha-256 36host replication all ::1/128 scram-sha-256
EC2の場合
bash
1ubuntu@ip-x-x-x-x:~$ cat /etc/os-release 2PRETTY_NAME="Ubuntu 22.04.3 LTS" 3NAME="Ubuntu" 4VERSION_ID="22.04" 5VERSION="22.04.3 LTS (Jammy Jellyfish)" 6VERSION_CODENAME=jammy 7ID=ubuntu 8ID_LIKE=debian 9HOME_URL="https://www.ubuntu.com/" 10SUPPORT_URL="https://help.ubuntu.com/" 11BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" 12PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" 13UBUNTU_CODENAME=jammy 14 15# ========================================= 16psql -V 17psql (PostgreSQL) 14.10 (Ubuntu 14.10-0ubuntu0.22.04.1) 18 19sudo nano /etc/postgresql/14/main/pg_hba.conf 20 21# ... 22local all postgres peer 23 24# TYPE DATABASE USER ADDRESS METHOD 25 26# "local" is for Unix domain socket connections only 27local all all peer 28# IPv4 local connections: 29host all all 127.0.0.1/32 scram-sha-256 30# IPv6 local connections: 31host all all ::1/128 scram-sha-256 32# Allow replication connections from localhost, by a user with the 33# replication privilege. 34local replication all peer 35host replication all 127.0.0.1/32 scram-sha-256 36host replication all ::1/128 scram-sha-256
補足
WSLでは、Ubuntuシステム上に以下の手順でユーザーubuntu
というを作成し、システムのデフォルトユーザーを変更してあります。
bash
1sudo adduser ubuntu 2sudo gpasswd -a ubuntu sudo 3 4sudo nano /etc/wsl.conf 5# 以下追記 6[user] 7default=ubuntu
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。