Streamlit + Postgres on Docker

Dear community,

I’m new to the Streamlit, and also Postgres. I have searched for answer on how to connect and get the data from Postgres while both are in docker containers, but not successfully.

I have successfully connected them on local machine wihout docker, but when they are deployed on the same machine on docker, not working.

Here is the docker-compose file:

version: '1.0'

services:
  postgres:
    image: postgres
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: admin
      POSTGRES_DB: myDataBase
    volumes:
      - postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
  pgadmin:
    image: dpage/pgadmin4
    restart: always
    environment:
      PGADMIN_DEFAULT_EMAIL: admin@email.com
      PGADMIN_DEFAULT_PASSWORD: admin
    ports:
      - "5050:80"

volumes:
  postgres-data:

Here is my secrects.toml:

[connections.postgresql]
dialect = "postgresql"
host = "postgres"
port = "5432"
database = "myDataBase"
username = "admin"
password = "admin"

Code in the app is:

# Initialize connection.
    conn = st.experimental_connection("postgresql", type="sql")
    # Perform query.
    df = conn.query('SELECT * FROM myTable;')

Error:

OperationalError: (psycopg2.OperationalError) could not translate host name "postgres" to address: Name or service not known (Background on this error at: https://sqlalche.me/e/20/e3q8)

Does anyone know what should be configured differently, because those settings are not working?

I have been able to deploy the app with the db to docker, and it can get the data from postgres.
It’s the problem with accessing the container outside the docker network. Still didn’t resolve how to access it. If anyone know how please assist. For now I will go with this approach.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.