DEV Community

Riverfunt
Riverfunt

Posted on • Edited on

3 1

Access the PostgreSQL in Docker Container

The use of Docker containers made our lives so easy. Not only the work of the sysadmin but work of the developer too.

However, these days I had to access the PostgreSQL that runs in a Docker container and had a bit of trouble, by inexperience and lack of knowledge until then.

Well, all is hard when we don't have the knowledge to do it. And to access the PostgreSQL in a Docker container is a piece of cake.

Firstly, we need to check if the container is up! We can do it with the following command line:

docker container ps
Enter fullscreen mode Exit fullscreen mode

The answer to this command is:

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES

56fa1b0f91d7        ba5ab22916de        "docker-entrypoint.s…"   24 hours ago        Up 5 minutes        0.0.0.0:5442->5432/tcp   my_database
Enter fullscreen mode Exit fullscreen mode

With the container running, as shown above, it is enough to run the following command line:

docker exec -it my_database bash
Enter fullscreen mode Exit fullscreen mode

Where my_database, in this case, is the Docker container NAME. We cuold use the container ID instead of the container NAME. In this context, we enter the container as the root user and now we can run the following command line:

psql -U postgres
Enter fullscreen mode Exit fullscreen mode

Ready! We are in PostgreSQL and we can create users, databases, tables and all we need to do our work. This fact is demonstrated by the change of the prompt to:

postgres=#
Enter fullscreen mode Exit fullscreen mode

I hope this small tip helps another person the same way it helped me as well.

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (1)

Collapse
 
biriyanist profile image
Ameen

Good read!
How can I access the same in one of the localhost port?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️