K
K
keddad2020-06-28 18:11:18
PostgreSQL
keddad, 2020-06-28 18:11:18

Why can a Docker-raised Postgres refuse to connect?

Task: raise Postgres and connect to it from a script that runs on the same machine, but outside the docker. docker-compose:

version: "3"
services:
  db:
    image: "postgres"
    environment:
      - POSTGRES_PASSWORD=pass
    container_name: "my_postgres"
    ports:
      - "54320:5432"


The server successfully rises and reports that it is ready to accept connections

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:

But when I try to connect to it, I catch 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:[email protected]:5432')


ufw is disabled, there can be no problems with the firewall. Where do I spit?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-06-28
@keddad

because you hang it on port 54320, and try to connect to 5432

S
Sergey, 2020-06-28
@KingstonKMS

You have specified a port to connect from the host, but you are using a different port in the script

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question