概要
could not connect to server: Connection refused は、PostgreSQLサーバーへの接続が拒否された場合に発生するエラーです。サーバーが起動していない、ポート設定の問題、ファイアウォール設定などが原因です。
エラーメッセージ
psql: error: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
FATAL: password authentication failed for user "postgres"
FATAL: no pg_hba.conf entry for host "192.168.1.100"
原因
- PostgreSQLが起動していない: サービスが停止している
- ポート番号の誤り: 5432以外のポートで起動している
- ホスト設定の問題: localhost/127.0.0.1/ソケットの違い
- pg_hba.conf の設定: 接続が許可されていない
- ファイアウォール: ポートがブロックされている
解決策
1. PostgreSQLサービスの確認・起動
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| # サービス状態を確認
sudo systemctl status postgresql
# サービスを起動
sudo systemctl start postgresql
# 自動起動を有効化
sudo systemctl enable postgresql
# macOS (Homebrew)
brew services list
brew services start postgresql@14
# ログを確認
sudo tail -f /var/log/postgresql/postgresql-14-main.log
|
2. 接続情報の確認
1
2
3
4
5
6
7
8
9
10
| # 接続テスト
psql -h localhost -p 5432 -U postgres -d postgres
# ソケット接続
psql -U postgres -d postgres
# 環境変数を確認
echo $PGHOST
echo $PGPORT
echo $PGUSER
|
3. postgresql.conf の設定
# /etc/postgresql/14/main/postgresql.conf
# リッスンするアドレス
listen_addresses = 'localhost' # localhost のみ
listen_addresses = '*' # すべてのIPから接続許可
listen_addresses = '0.0.0.0' # IPv4 のみ
# ポート番号
port = 5432
# 設定を確認
1
2
3
4
5
6
7
| # 設定ファイルの場所を確認
psql -U postgres -c "SHOW config_file;"
psql -U postgres -c "SHOW hba_file;"
# 現在の設定を確認
psql -U postgres -c "SHOW listen_addresses;"
psql -U postgres -c "SHOW port;"
|
4. pg_hba.conf の設定
# /etc/postgresql/14/main/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# ローカル接続(ソケット)
local all all peer
# IPv4 ローカル接続
host all all 127.0.0.1/32 scram-sha-256
# すべてのIPからの接続を許可(開発環境用)
host all all 0.0.0.0/0 scram-sha-256
# 特定のネットワークからの接続
host all all 192.168.1.0/24 scram-sha-256
# 認証方式:
# peer: OSのユーザー名で認証(ローカルのみ)
# scram-sha-256: パスワード認証(推奨)
# md5: パスワード認証(旧式)
# trust: 認証なし(開発環境のみ)
1
2
3
4
5
6
| # 設定変更後、再起動
sudo systemctl restart postgresql
# または設定のリロード
sudo systemctl reload postgresql
psql -U postgres -c "SELECT pg_reload_conf();"
|
5. ファイアウォールの設定
1
2
3
4
5
6
7
8
9
10
| # Ubuntu (ufw)
sudo ufw allow 5432/tcp
sudo ufw status
# CentOS (firewalld)
sudo firewall-cmd --add-port=5432/tcp --permanent
sudo firewall-cmd --reload
# AWS Security Group
# インバウンドルールで5432ポートを許可
|
6. Docker での設定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| # docker-compose.yml
version: '3.8'
services:
app:
build: .
depends_on:
- db
environment:
DATABASE_URL: postgres://postgres:password@db:5432/myapp
db:
image: postgres:14
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_DB: myapp
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
volumes:
postgres_data:
|
1
2
3
4
5
| # Docker コンテナ内から接続
docker exec -it postgres_container psql -U postgres
# ホストから Docker の PostgreSQL に接続
psql -h localhost -p 5432 -U postgres
|
7. パスワード認証の問題
1
2
3
4
5
6
7
| # postgres ユーザーのパスワードを設定
sudo -u postgres psql
ALTER USER postgres PASSWORD 'new_password';
\q
# 新しいパスワードで接続
psql -h localhost -U postgres -W
|
8. ソケットファイルの問題
1
2
3
4
5
6
7
8
9
10
11
12
| # ソケットファイルの場所を確認
psql -U postgres -c "SHOW unix_socket_directories;"
# 一般的な場所
# /var/run/postgresql
# /tmp
# ソケットの権限を確認
ls -la /var/run/postgresql/
# ソケットがない場合、tcp/ip 接続を使用
psql -h 127.0.0.1 -U postgres
|
9. SSL 接続の問題
1
2
3
4
5
| # SSL なしで接続
psql "host=localhost dbname=myapp user=postgres sslmode=disable"
# SSL 必須の場合
psql "host=localhost dbname=myapp user=postgres sslmode=require"
|
# postgresql.conf
ssl = on
ssl_cert_file = '/etc/ssl/certs/server.crt'
ssl_key_file = '/etc/ssl/private/server.key'
デバッグのコツ
接続の詳細ログ
1
2
3
4
5
| # 詳細な接続情報を表示
psql -h localhost -U postgres -d postgres -c "SELECT version();" -a
# libpq のデバッグ
PGOPTIONS='-c log_connections=on' psql -h localhost -U postgres
|
ポートの確認
1
2
3
4
5
6
| # PostgreSQL が使用しているポートを確認
sudo netstat -tlnp | grep postgres
sudo lsof -i :5432
# プロセスを確認
ps aux | grep postgres
|
ログの確認
1
2
3
4
5
| # PostgreSQL ログ
sudo tail -f /var/log/postgresql/postgresql-14-main.log
# journalctl
sudo journalctl -u postgresql -f
|