1

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

1
  • 2
    I guess the problem is that the Postgres is running in another container and you should use that container's address to connect to it (and not localhost). Check this: docs.docker.com/compose/networking Commented Mar 22, 2023 at 10:53

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.