11
8

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に「psql -U <ユーザ名> -h <ホスト名>」で接続しようとすると、「psql: FATAL: database "<ユーザ名>" does not exist」とエラーが起きる

Last updated at Posted at 2020-08-27

#はじめに
PostgreSQL接続時に、入力した値は間違っていないのに接続エラーが
発生したので、解決策をメモします。

【DB接続情報】


DB_HOST=postgres
DB_USERNAME=default
DB_PASSWORD=secret

PostgreSQLに接続するために以下のようにコマンドを打ち、

$ psql -U default -h postgres

(ユーザ名:default、 ホスト名:postgres)

続いてパスワードを求められるので、

Password for user default: 

ここに設定しているパスワード「secret」を入力したところ、
エラーが発生。

psql: FATAL:  database "default" does not exist

ユーザ名として入力した「default」というデータベースが存在しない・・?

#解決策

-dオプションで、-d <データベース名>をコマンドを追加したところ、
無事に接続することができました。

$ psql -U default -h postgres -d postgres

(今回接続するDB名は「postgres」)

どうやら、-dオプションでDB名を指定しないと、ユーザ名と同名のDBを参照する
こともあるようです。
今回は、ユーザ名に「default」を指定しましたが、同名のDBは存在していなかったので
エラーが発生したと考えられます。

11
8
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
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?