2

I have Django application with postgresql on docker.

I tried to insert data like this:

import psycopg2

try:
    connection = psycopg2.connect(user="postgres",
                                  password="postgres",
                                  host="127.0.0.1",
                                  port="5432",
                                  database="postgres_db")
    cursor = connection.cursor()

    postgres_insert_query = """ INSERT INTO Test (smth1, smth2) VALUES (%s,%s)"""
    record_to_insert = (5, 950)
    cursor.execute(postgres_insert_query, record_to_insert)

    connection.commit()
    count = cursor.rowcount
    print(count, "Record inserted successfully into mobile table")

except (Exception, psycopg2.Error) as error:
    print("Failed to insert record into mobile table", error)

finally:
    # closing database connection.
    if connection:
        cursor.close()
        connection.close()
        print("PostgreSQL connection is closed")

but I have this error: Connection refused Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

In my postgresql.conf file I have this lines: listen_addresses = '*' and port = 5432

My pg_hba.conf file looks like this:

 TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            trust
# IPv6 local connections:
host    all             all             ::1/128                 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     trust
host    replication     all             127.0.0.1/32            trust
host    replication     all             ::1/128                 trust

host all all all md5

How can I fix this error and insert data into database?

EDIT 1 - I have added docker-compose.yml:

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
      - ./tab1.sql:/docker-entrypoint-initdb.d/tab1.sql
      - ./tab1]2.sql:/docker-entrypoint-initdb.d/tab2.sql
    environment:
      - POSTGRES_DB=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - PGDATA=/var/lib/postgresql/data
  
  pgadmin:
    image: dpage/pgadmin4:4.18
    restart: unless-stopped
    environment:
      - PGADMIN_DEFAULT_EMAIL=admin@domain.com
      - PGADMIN_DEFAULT_PASSWORD=admin
      - PGADMIN_LISTEN_PORT=80
    ports:
      - "8090:80"
    volumes:
      - ./pgadmin-data:/var/lib/pgadmin
    links:
      - "db:pgsql-server"


  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

volumes:
  db-data:
  pgadmin-data:

This is my docker compose file. I'm using it to run my Djagno application.

  • 1
    You mentioned you run that script from your host machine, that cases is expected to fail because you are not exposing ports. That script will work only if it is exected in the postgres container. If you want to run it in a different container, you will have to change `host="127.0.0.1"` to `host='postgres'`. – snahor May 11 '21 at 04:12
  • @snahor when I change it in `connect` function I've got this error message `could not translate host name "postgres" to address: Name or service not known`. – tryToRunThisCommand May 11 '21 at 04:28
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/232197/discussion-between-snahor-and-trytorunthiscommand). – snahor May 11 '21 at 04:49
  • Does this answer your question? [How to Connect to Docker Postgres Container from Host Machine](https://stackoverflow.com/questions/48385824/how-to-connect-to-docker-postgres-container-from-host-machine) – aaron May 01 '22 at 10:49

0 Answers0