Connect to PostgreSQL Database inside Docker Container

Benno
Mar 26, 2023

--

Find the name or ID of the container running the PostgreSQL database. You can use the docker ps command to list all running containers and their IDs and names.

$ docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e4972eb9d0e9 postgres:12-alpine "docker-entrypoint.s…" 26 minutes ago Up 26 minutes 0.0.0.0:5432->5432/tcp my-postgres-db

Use the docker exec command to start a shell inside the container. Replace “container_name_or_id” with the name or ID of the container.

$ docker exec -it container_name_or_id bash

Once you are inside the container’s shell, run the psql command to connect to the PostgreSQL database. Replace “username” and “database_name” with your database credentials.

$ psql -U username -d database_name

Thats it :)

cheers

--

--

Responses (1)