How to Access a PostgreSQL Database in a Docker Container



If Docker is installed on a remote server, you may need to access the server using SSH:

ssh user_name@ip_address

If Docker is installed in your local environment, you can directly open a terminal and execute further commands. First, list all Docker containers using this command:

docker ps -a

Find the name of your PostgreSQL DB installation and use this command to access it:

docker exec -it db_container_name bash

Now you can execute PostgreSQL commands as needed. To access the PostgreSQL shell:

psql -U postgres

For exporting the database:

pg_dump -U postgres -d db_name > db_name.sql

For importing the database:

psql -U postgres -d db_name -f db_name.sql

To view logs of this container, exit from the container first, and then execute:

docker logs -f db_container_name








Comments

Popular Posts