できない.dev

PostgreSQL に「Connection refused」で接続できない(port 5432)

PostgreSQL の Connection refused は、サーバーを起動し、listen_addresses とポート番号を接続先に合わせれば直る。
その host とポートで待ち受けるプロセスが無いと出る。

公開: 更新:

症状できない · exit 2
psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT version();"
psql: error: connection to server at "127.0.0.1", port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
解決後できた · exit 0
psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT version();"
                                                       version
----------------------------------------------------------------------------------------------------------------------
 PostgreSQL 17.11 (Debian 17.11-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
(1 row)

2026-10-16 に postgres:17(PostgreSQL 17.11) で実際に打って取った出力。検証環境の詳細

要約

connection to server at "127.0.0.1", port 5432 failed: Connection refused と Is the server running on that host and accepting TCP/IP connections? は、その host:port に待ち受けているプロセスが無いことを意味する。
libpq 13 以前のクライアントでは、同じ状態が could not connect to server: Connection refused と Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? の形で出る。
認証より手前の段階で弾かれている。
サーバーが起動しているか、listen_addresses が対象 IP を含むか、ポートが合っているかを順に潰す。

実行例

postgres:17 のコンテナに新しいクラスタを作り、サーバーを起動しないまま psql を 5432 に向けると Connection refused(終了コード 2)になり、pg_ctl で起動すると同じコマンドで PostgreSQL 17.11 のバージョンが返った。
待ち受けポートを 5433 にした場合や listen_addresses を空にした場合も 5432 では拒否され、実際のポートを SHOW port と Unix ソケットのファイル名で確かめて合わせる、あるいは listen_addresses を '*' にして再起動すると解消している。

$ psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT version();"
psql: error: connection to server at "127.0.0.1", port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
$ echo $?
2
$ gosu postgres pg_ctl -D /tmp/pgdata -o "-c listen_addresses=localhost" -l /tmp/pgdata/server.log -w start
waiting for server to start.... done
server started
$ echo $?
0
$ psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT version();"
                                                       version                                                        
----------------------------------------------------------------------------------------------------------------------
 PostgreSQL 17.11 (Debian 17.11-1.pgdg13+2) on x86_64-pc-linux-gnu, compiled by gcc (Debian 14.2.0-19) 14.2.0, 64-bit
(1 row)
$ echo $?
0
$ psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT 1;"
psql: error: connection to server at "127.0.0.1", port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
$ echo $?
2
$ ls -a /var/run/postgresql
.
..
.s.PGSQL.5433
.s.PGSQL.5433.lock
$ psql -h /var/run/postgresql -p 5433 -U postgres -c "SHOW port;"
 port 
------
 5433
(1 row)
$ psql -h 127.0.0.1 -p 5433 -U postgres -c "SELECT 1;"
 ?column? 
----------
        1
(1 row)
$ echo $?
0
$ psql -h 127.0.0.1 -p 5432 -U postgres -c "SELECT 1;"
psql: error: connection to server at "127.0.0.1", port 5432 failed: Connection refused
	Is the server running on that host and accepting TCP/IP connections?
$ echo $?
2
$ psql -h 127.0.0.1 -p 5432 -U postgres -c "SHOW listen_addresses;"
 listen_addresses 
------------------
 *
(1 row)
$ echo $?
0

— 2026-10-16 時点の出力

検証環境PostgreSQL 17.11 / 2026-10-16 検証
検証日
実行環境
postgres:17 Debian GNU/Linux 13 (trixie)
バージョン
  • PostgreSQL 17.11

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. サーバーを起動する

環境ごとに起動方法が異なる。

# systemd
sudo systemctl start postgresql
# Homebrew (macOS)
brew services start postgresql@16

2. listen_addresses と port を設定して再起動する

外部や別インターフェイスから繋ぐ場合は postgresql.conf を変更する。

listen_addresses = '*'
port = 5432

listen_addresses の変更は reload では反映されず、再起動が必要になる(Connections and Authentication(新しいタブで開く))。
あわせて pg_hba.conf で接続元を許可する。

3. Docker ならポートを publish する

docker run -e POSTGRES_PASSWORD=secret -p 5432:5432 postgres:16

ホスト側からは localhost:5432 で届くようになる。
コンテナ間なら相手はサービス名で解決する。

よくある原因

  1. サーバーが起動していない。
    インストール直後や再起動後で、サービスが上がっていない。
  2. listen_addresses が待ち受け先を制限している。
    既定は localhost のため、別ホストや別インターフェイスからは待ち受けていない。
  3. ポートが違う。
    既定は 5432 だが、複数バージョン同居や設定変更で別ポートになっている。
  4. Docker でポートを公開していない。
    コンテナ内では動いていても、-p 5432:5432 が無いとホストから届かない。

よくある原因の切り分け

まず本当に起動しているか、どのポートで待っているかを確認する。

# Linux: 待ち受けポートとプロセスを確認
ss -ltnp | grep 5432
# サービス状態(systemd の例)
systemctl status postgresql