GrepBoard/매뉴얼
605 편 ·

PostgreSQL 5432 Connection refused 해결

·시험 환경 PostgreSQL 17.11 (Docker postgres:17) · iproute2 6.15 · 2026-10·시험 범위 ↓·7 단계

psql Connection refused 는 그 주소·포트에 듣는 서버가 없다는 뜻입니다. 서버·포트·listen_addresses 를 보고, 재시작한 뒤 pg_hba.conf 까지 엽니다.

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

그 주소·포트에서 연결을 받는 프로세스가 없어 운영체제가 바로 거절한 것입니다. PostgreSQL 의 인증까지 가지도 못했으므로 비밀번호·pg_hba.conf 와는 아직 상관없습니다. 서버가 떠 있는지, 포트가 맞는지, listen_addresses 가 그 주소를 듣는지 차례로 보면 됩니다. PostgreSQL 기본값 localhost 는 같은 기계의 루프백 접속만 받습니다.

[1] 서버가 떠 있는지 봅니다. 서버가 꺼져 있으면 TCP 와 소켓 문구가 이렇게 다릅니다.

console
$ psql -h 127.0.0.1 -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?$ psql -U postgres -c 'SELECT 1'psql: error: connection to server on socket "/var/run/postgresql/.s.PGSQL.5432" failed: No such file or directory	Is the server running locally and accepting connections on that socket?

서버 기계에서 소켓 쪽도 No such file or directory 면 서버가 꺼져 있거나 다른 소켓 디렉터리를 씁니다. 서비스 상태와 서버 로그부터 봅니다. 소켓으로는 붙는데 TCP 만 거절되면 아래로 넘어갑니다.

[2] 포트를 확인합니다. 다른 포트로 붙으면 같은 문구가 나오고, 문구에 시도한 포트가 찍힙니다.

console
$ psql -h 127.0.0.1 -U postgres -p 5433 -c 'SELECT 1'psql: error: connection to server at "127.0.0.1", port 5433 failed: Connection refused	Is the server running on that host and accepting TCP/IP connections?$ psql -h 127.0.0.1 -U postgres -Atc 'SHOW port'5432

클라이언트 설정의 포트를 서버의 SHOW port 값과 맞춥니다.

[3] 서버가 어느 주소에서 듣는지 서버 기계에서 봅니다.

console
$ ss -ltnState  Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0      200        127.0.0.1:5432      0.0.0.0:*   LISTEN 0      200            [::1]:5432         [::]:*   $ psql -h 127.0.0.1 -U postgres -Atc 'SHOW listen_addresses'localhost$ psql -h 192.0.2.10 -U postgres -c 'SELECT 1'psql: error: connection to server at "192.0.2.10", port 5432 failed: Connection refused	Is the server running on that host and accepting TCP/IP connections?

127.0.0.1:5432 · [::1]:5432 만 있으면 바깥 주소로 온 연결은 받지 않습니다. 같은 기계에서 자기 IP 로 붙어도 거절됩니다.

[4] listen_addresses 를 바꿉니다. 특정 주소만 받으려면 'localhost,192.0.2.10', 모든 주소면 '*' 입니다. ALTER SYSTEM 은 postgresql.auto.conf 에 써서 postgresql.conf 보다 우선합니다.

console
$ psql -h 127.0.0.1 -U postgres -c "ALTER SYSTEM SET listen_addresses = '*'"ALTER SYSTEM$ psql -h 127.0.0.1 -U postgres -Atc 'SELECT pg_reload_conf()'t$ psql -h 127.0.0.1 -U postgres -c "SELECT name, setting, pending_restart FROM pg_settings WHERE name = 'listen_addresses'"       name       |  setting  | pending_restart ------------------+-----------+----------------- listen_addresses | localhost | t(1 row)

다시 읽기(reload)로는 바뀌지 않습니다. pending_restart 가 t 이고 값은 여전히 localhost 입니다. 이 설정은 서버 시작 때만 정해진다고 공식 문서가 적고 있습니다.

[5] 서버를 재시작하고 듣는 주소를 다시 봅니다. 재시작하면 연결이 모두 끊기니 접속이 적은 때에 합니다. 패키지 설치본은 보통 서비스 관리자(systemctl restart postgresql 등)로, 아래는 pg_ctl 로 직접 띄운 경우입니다.

console
$ su postgres -c 'pg_ctl -D /var/lib/postgresql/17/main -l /tmp/pg.log -w restart'waiting for server to shut down.... doneserver stoppedwaiting for server to start.... doneserver started$ ss -ltnState  Recv-Q Send-Q Local Address:Port Peer Address:PortLISTEN 0      200          0.0.0.0:5432      0.0.0.0:*   LISTEN 0      200             [::]:5432         [::]:*   $ psql -h 192.0.2.10 -U postgres -c 'SELECT 1'psql: error: connection to server at "192.0.2.10", port 5432 failed: FATAL:  no pg_hba.conf entry for host "192.0.2.10", user "postgres", database "postgres", no encryption

이제 Connection refused 가 아니라 no pg_hba.conf entry 가 나옵니다. 연결은 닿았고 인증 규칙에 그 주소가 없다는 뜻이라, 한 단계 나아간 것입니다.

[6] 그 주소(또는 대역)를 pg_hba.conf 에 넣고 다시 읽게 합니다. 이 파일은 재시작 없이 reload 로 적용됩니다. scram-sha-256 규칙은 비밀번호 인증이라, 접속할 역할에 비밀번호가 미리 있어야 합니다. 이 재현의 TCP 접속은 모두 postgres 역할의 시험용 비밀번호(initdb 때 넣은 값)를 PGPASSWORD 환경 변수로 준 상태입니다. 직접 칠 때는 비밀번호 프롬프트(psql -W)에 입력하거나 권한 600 의 ~/.pgpass 를 씁니다.

console
$ tail -1 /var/lib/postgresql/17/main/pg_hba.confhost    all             all             192.0.2.10/32            scram-sha-256$ psql -h 127.0.0.1 -U postgres -Atc 'SELECT pg_reload_conf()'t$ psql -h 192.0.2.10 -U postgres -c 'SELECT inet_server_addr(), inet_client_addr()' inet_server_addr | inet_client_addr ------------------+------------------ 192.0.2.10       | 192.0.2.10(1 row)

실제로는 클라이언트 대역(예 203.0.113.0/24)을 적습니다. 0.0.0.0/0 은 모든 곳에서 비밀번호 추측을 받게 되니 피합니다. 소켓 접속의 Peer authentication failed 는 다른 문제입니다 — PostgreSQL Peer authentication failed 해결.

[7] 설정을 바꿨는데 값이 엉뚱하면 어디서 왔는지 봅니다. postgresql.conf 를 고쳐도 postgresql.auto.conf 에 남은 ALTER SYSTEM 값이 이깁니다.

console
$ grep -n listen_addresses /var/lib/postgresql/17/main/postgresql.conf /var/lib/postgresql/17/main/postgresql.auto.conf/var/lib/postgresql/17/main/postgresql.conf:60:listen_addresses = 'localhost'/var/lib/postgresql/17/main/postgresql.auto.conf:3:listen_addresses = '*'$ psql -h 127.0.0.1 -U postgres -c "SELECT setting, source, sourcefile FROM pg_settings WHERE name = 'listen_addresses'" setting |       source       |                    sourcefile                    ---------+--------------------+-------------------------------------------------- *       | configuration file | /var/lib/postgresql/17/main/postgresql.auto.conf(1 row)

공식 근거: listen_addresses · port · ALTER SYSTEM · pg_settings · pg_hba.conf · docker-library/postgres Dockerfile

pg_hba.conf 규칙 대조기 도구로 바로 확인 ↗ — 브라우저 안에서 계산합니다.

#postgresql#psql#listen_addresses#pg_hba