3
2

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 1 year has passed since last update.

psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed

Last updated at Posted at 2023-05-31

概要

  • 下記エラーを解消したい
    psql: error: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused Is the server running on that host and accepting TCP/IP connections?

環境

  • Ubuntu 18.04.6 LTS
  • Postgresqlのパスワードは設定済み
  • create databaseも実行済み

ファイル修正

postgresql.confを探す

find / -name postgresql.conf 2> /dev/null

以下に変更

# - Connection Settings -

# listen_addresses = '*'          # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
port = 5432                             # (change requires restart)

リスタートする

sudo service postgresql restart

こちらのファイルも変更する

/etc/postgresql/14/main/pg_hba.conf

開くとこのようになっているので、

# IPv4 local connections:
host    all             all             127.0.0.1/32         scram-sha-256

以下のように変更

# IPv4 local connections:
host    all             all             0.0.0.0/0            scram-sha-256

リスタートする

sudo service postgresql restart

再度接続

psql -h localhost -U postgres -W test
psql (14.8 (Ubuntu 14.8-1.pgdg18.04+1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.

test=#

できた!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?