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
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
With the container running, as shown above, it is enough to run the following command line:
docker exec -it my_database bash
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
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:
I hope this small tip helps another person the same way it helped me as well.
Oldest comments (1)
How can I access the same in one of the localhost port?