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

pgAdmin4:サーバに接続できません:

Posted at

flaskの環境構築備忘録です。

😞問題

flaskとデータベースをpgAdmin4を使って❌接続できない

💻環境&前提

  • macOS Sequoia 15.5
  • brew経由でPostgreSQLをインストール済

❌出力されたエラー文

pgAdmin4で新しいサーバー情報を保存しようとすると下記エラー文が出る。

 サーバに接続できません:

connection failed: connection to server at "127.0.0.1", port 5432 failed: FATAL: role "postgres" does not exist
Multiple connection attempts failed. All failures were:
- host: 'localhost', port: '5432', hostaddr: '::1': connection failed: connection to server at "::1", port 5432 failed: FATAL: role "postgres" does not exist
- host: 'localhost', port: '5432', hostaddr: '127.0.0.1': connection failed: connection to server at "127.0.0.1", port 5432 failed: FATAL: role "postgres" does not exist 

🔍問題の本質

FATAL: role "postgres" does not exist

pgAdmin4でログインしようとしている「ユーザー名(role)」がpostgresであるのに、そのユーザーがデータベース側に存在しない、というエラー。

✅解決方法

方法1:postgres ロールを作成する(推奨) ⬅️こちらを実施した

ターミナルを開いて、PostgreSQLにpsqlでログイン(macOSユーザー名と同じ名前のロールで入る)
PostgreSQL を初めてセットアップした直後は、macOSのログインユーザー名と同じ名前のロールが自動的に作成されている場合が多い。
まずはそれでログインできるか試す。

psql postgres

接続ができた場合、次のようなプロンプトが表示される:

postgres=#

※ 上記で接続できない場合は、以下で指定してみる(ex.自分のmacOSのユーザー名がyournameなら)

psql -U yourname -d postgres

以下のSQLを実行してpostgresロールを作成:

CREATE ROLE postgres WITH LOGIN SUPERUSER PASSWORD '任意のパスワード';

※ パスワードを設定した場合はあとで pgAdmin4 に入力する必要がある。

方法2:自分のmacOSのユーザー名に合わせてpgAdmin4を設定する

PostgreSQLはインストール直後、macOSの現在のログインユーザーと同じ名前のロールだけが存在している場合が多い。
そのため、pgAdmin4の「接続」タブで以下のように設定する:

  • ユーザー名:macOSのログインユーザー名(whoamiで確認できます)
  • パスワード:そのロールに設定されたもの(未設定なら空でOK)

➕補足:ロール確認&終了コマンド

どのロールが存在するか確認するには、psqlに入って以下を実行してください:

\du

psqlを終了する

\q

🔆まとめ

原因 解決策
postgres ロールが存在しない CREATE ROLE postgres WITH LOGIN...で作成する
ログイン名が間違っている 自分のmacOSユーザー名に設定を変更する
パスワードが間違っている 正しいパスワードを確認・設定する

❤️情報源

ChatGPT

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