Задача: поднять Постгрес и подключиться к нему из скрипта, который крутится на той же машине, но вне докера.
docker-compose
:
version: "3"
services:
db:
image: "postgres"
environment:
- POSTGRES_PASSWORD=pass
container_name: "my_postgres"
ports:
- "54320:5432"
Сервер успешно поднимается и сообщает о том, что готов к принятию соединений
my_postgres |
my_postgres | PostgreSQL Database directory appears to contain a database; Skipping initialization
my_postgres |
my_postgres | 2020-06-28 15:04:08.490 UTC [1] LOG: starting PostgreSQL 12.3 (Debian 12.3-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
my_postgres | 2020-06-28 15:04:08.490 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
my_postgres | 2020-06-28 15:04:08.490 UTC [1] LOG: listening on IPv6 address "::", port 5432
my_postgres | 2020-06-28 15:04:08.492 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
my_postgres | 2020-06-28 15:04:08.505 UTC [25] LOG: database system was shut down at 2020-06-28 15:04:06 UTC
my_postgres | 2020-06-28 15:04:08.510 UTC [1] LOG: database system is ready to accept connections на порте 5432:
Но когда я пытаюсь подключиться к нему, я ловлю Connection refused:
Exception has occurred: ClientCannotConnectError
could not establish connection to server
CODE: 08001
LOCATION: CLIENT
CONNECTION: [failed]
failures[0]:
socket('127.0.0.1', 5432)
Traceback (most recent call last):
File "/home/keddad/Documents/postgres-vectors/.env/lib/python3.8/site-packages/postgresql/protocol/client3.py", line 136, in connect
self.socket = self.socket_factory(timeout = timeout)
File "/home/keddad/Documents/postgres-vectors/.env/lib/python3.8/site-packages/postgresql/python/socket.py", line 64, in __call__
s.connect(self.socket_connect)
ConnectionRefusedError: [Errno 111] Connection refused
The above exception was the direct cause of the following exception:
postgresql.exceptions.ConnectionRejectionError: Connection refused
CODE: 08004
LOCATION: CLIENT
CONNECTOR: [Host] pq://postgres:***@localhost:5432
category: None
DRIVER: postgresql.driver.pq3.Driver
File "/home/keddad/Documents/postgres-vectors/simple_kd.py", line 26, in <module>
db = postgresql.open('pq://postgres:pass@localhost:5432')
ufw отключен, проблем с файерволом быть не может. Где я косячу?