user21214981
user21214981

Reputation:

Can't connect to Postgres database base using python in docker

When run command docker-compose up, I get a message in the log Traceback (most recent call last): test_assigned-print_data-1 | File "/project/print_data.py", line 3, in test_assigned-print_data-1 | connect = psycopg2.connect( test_assigned-print_data-1 | ^^^^^^^^^^^^^^^^^ test_assigned-print_data-1 | File "/usr/local/lib/python3.11/site-packages/psycopg2/init.py", line 122, in connect test_assigned-print_data-1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync) test_assigned-print_data-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ test_assigned-print_data-1 | psycopg2.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused test_assigned-print_data-1 | Is the server running on that host and accepting TCP/IP connections? test_assigned-print_data-1 | connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address test_assigned-print_data-1 | Is the server running on that host and accepting TCP/IP connections?

Docker-compose file

version: '3.9'

services:
  db:
    container_name: db
    image: postgres:latest
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_HOST: localhost


  get_data:
    build: .
    command: python3 get_data.py
    depends_on:
      - db

  print_data:
    build: .
    command: python3 print_data.py
    depends_on:
      - db


Python file

import psycopg2

connect = psycopg2.connect(
    dbname="postgres",
    user="postgres",
    password="postgres",
    host="localhost",
    port="5432"
)

cur = connect.cursor()

# Execute an SQL query to aggregate the data by region
cur.execute("""
    SELECT region,
        SUM(population) AS total_population,
        MAX(country) FILTER (WHERE population = max_pop) AS largest_country,
        MAX(population) AS largest_population,
        MIN(country) FILTER (WHERE population = min_pop) AS smallest_country,
        MIN(population) AS smallest_population
    FROM (
        SELECT region, country, population,
            MAX(population) OVER (PARTITION BY region) AS max_pop,
            MIN(population) OVER (PARTITION BY region) AS min_pop
        FROM population_data
    ) subquery
    GROUP BY region
""")

# Fetch the results and print
results = cur.fetchall()
for row in results:
    region, total_population, largest_country, largest_population, smallest_country, smallest_population = row
    print(f"Region name{region}")
    print(f"Total population: {total_population:,}")
    print(f"Largest country: {largest_country:,}")
    print(f"Population of a largest country{largest_population:,}")
    print(f"Smallest country: {smallest_country:,}")
    print(f"Population of a smallest country{smallest_population:,}")

I'm trying to change ports, configure the "listening_address" line of postgres.conf. Trying to execute some commands in a terminal on MacOS

Upvotes: 1

Views: 156

Answers (1)

Maxim Ch.
Maxim Ch.

Reputation: 1

Perhaps changing the settings in the pg_hba.conf file will help you.

Try change like this:

# IPv4 local connections:
host    all             all             0.0.0.0/0            scram-sha-256

Upvotes: 0

Related Questions