Docker Community Forums

Share and learn in the Docker community.

Not able to connect to the user using Postgres python package

I have created a docker image of postgres along with Kafka stream using docker-compose.

version: "3"

services:
  zookeeper:
    image: confluentinc/cp-zookeeper:latest
    environment:
      ZOOKEEPER_CLIENT_PORT: 2181
      ZOOKEEPER_TICK_TIME: 2000
    networks:
      - stream-network
  kafka:
    image: confluentinc/cp-kafka:latest
    depends_on:
      - zookeeper
    ports:
      - 9092:9092
    environment:
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:29092,PLAINTEXT_HOST://localhost:9092
      KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      KAFKA_INTER_BROKER_LISTENER_NAME: PLAINTEXT
      KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 3
    networks: 
        - stream-network
  streamer:
    build:
      context: ./streamingProducer/
    networks: 
      - stream-network
    depends_on:
      - kafka
  consumer:
    build:
      context: ./streamingConsumer/
    networks: 
      - stream-network
    depends_on:
      - kafka
      - postgres  
  postgres:
    # image: postgres
    # restart: always
    # environment:
    #   POSTGRES_USER: docker
    #   POSTGRES_PASSWORD: docker
    #   POSTGRES_DB: test
    #   PGDATA: /var/lib/postgresql/data/pgdata
    # ports:
    #   - "5432:5432"
    # healthcheck:
    #   test: ["CMD-SHELL", "pg_isready -U docker"]
    # volumes:
    #   - ./data/postgres/pgdata:/var/lib/postgresql/data/pgdata
      build: './db'
      restart: always
      environment:
        - "FILLA_DB_USER=filla"
        - "FILLA_DB_PASSWORD=filla"
        - "FILLA_DB_DATABASE=filladb1"
        - "POSTGRES_USER=postgres"
        - "POSTGRES_PASSWORD=password"
      ports:
        - "5432:5432"
      networks: 
        - stream-network 

The docker image of Postgres seems to run fine, the log looks completely good

  • `postgres_1 | creating configuration files … ok
  • zookeeper_1 | ===> User
  • postgres_1 | running bootstrap script … ok
  • zookeeper_1 | uid=0(root) gid=0(root) groups=0(root)
  • zookeeper_1 | ===> Configuring …
  • postgres_1 | performing post-bootstrap initialization … sh: locale: not found
  • postgres_1 | 2020-05-27 11:52:45.209 UTC [29] WARNING: no usable system locales were found
  • slimmerai_consumer_1 exited with code 0
  • postgres_1 | ok
  • postgres_1 | syncing data to disk … ok
  • postgres_1 | initdb: warning:
  • postgres_1 | enabling “trust” authentication for local connections
  • postgres_1 | You can change this by editing pg_hba.conf or using the option -A, or
  • postgres_1 | --auth-local and --auth-host, the next time you run initdb.
  • postgres_1 |
  • postgres_1 | Success. You can now start the database server using:
  • postgres_1 |
  • postgres_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start
  • postgres_1 |
  • postgres_1 | waiting for server to start…2020-05-27 11:52:46.729 UTC [34] LOG: starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.2.0) 9.2.0, 64-bit
  • postgres_1 | 2020-05-27 11:52:46.736 UTC [34] LOG: listening on Unix socket “/var/run/postgresql/.s.PGSQL.5432”
  • postgres_1 | 2020-05-27 11:52:46.767 UTC [35] LOG: database system was shut down at 2020-05-27 11:52:46 UTC
  • postgres_1 | 2020-05-27 11:52:46.771 UTC [34] LOG: database system is ready to accept connections
  • postgres_1 | done
  • postgres_1 | server started
  • postgres_1 |
  • postgres_1 | /usr/local/bin/docker-entrypoint.sh: sourcing /docker-entrypoint-initdb.d/01-filladb.sh
  • postgres_1 | CREATE ROLE
  • postgres_1 | CREATE DATABASE
  • postgres_1 | GRANT
  • postgres_1 |
  • postgres_1 | 2020-05-27 11:52:46.989 UTC [34] LOG: received fast shutdown request
  • postgres_1 | waiting for server to shut down…2020-05-27 11:52:46.991 UTC [34] LOG: aborting any active transactions
  • postgres_1 | 2020-05-27 11:52:46.996 UTC [34] LOG: background worker “logical replication launcher” (PID 41) exited with exit code 1
  • postgres_1 | 2020-05-27 11:52:46.996 UTC [36] LOG: shutting down
  • postgres_1 | 2020-05-27 11:52:47.010 UTC [34] LOG: database system is shut down
  • postgres_1 | done
  • postgres_1 | server stopped
  • postgres_1 |
  • postgres_1 | PostgreSQL init process complete; ready for start up.
  • postgres_1 |
  • postgres_1 | 2020-05-27 11:52:47.100 UTC [1] LOG: starting PostgreSQL 12.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 9.2.0) 9.2.0, 64-bit
  • postgres_1 | 2020-05-27 11:52:47.101 UTC [1] LOG: listening on IPv4 address “0.0.0.0”, port 5432
  • postgres_1 | 2020-05-27 11:52:47.101 UTC [1] LOG: listening on IPv6 address “::”, port 5432
  • postgres_1 | 2020-05-27 11:52:47.104 UTC [1] LOG: listening on Unix socket “/var/run/postgresql/.s.PGSQL.5432”
  • postgres_1 | 2020-05-27 11:52:47.125 UTC [45] LOG: database system was shut down at 2020-05-27 11:52:47 UTC
  • postgres_1 | 2020-05-27 11:52:47.129 UTC [1] LOG: database system is ready to accept connections
  • `

And when I log into the database docker container, I can see the users and all the db being created.

But when I try to access the db using python package

from psycopg2 import Error

try:
connection = psycopg2.connect(user = "postgres",
password = "password",
host = "localhost",
port = "5432",
database = "filladb1")


cursor = connection.cursor()

print("Table connected successfully in PostgreSQL ")

except (Exception, psycopg2.DatabaseError) as error :
print (“Error while creating PostgreSQL table”, error)`

It throws an error:

Error while creating PostgreSQL table FATAL: role “postgres” does not exist

Not sure, what am I doing wrong, any help would be appreciated. Thanks.