Skip to content

[Docker] Access the PostgreSQL command line terminal through Docker

Radu Raicea edited this page Sep 30, 2017 · 1 revision

If you need to access the PostgreSQL database command line terminal, you need to go inside the postgres Docker container. There, you can run SQL command and edit the database.

Run the application

To enter the postgres container, the application must be running

docker-compose up --build

Entering the postgres container

Once the application is running, open a new Docker terminal or the equivalent, and run the following command

docker exec -it postgres psql -U postgres

This command runs the psql command (which is the command line for PostgreSQL) under the user postgres.

Connecting to a database

PostgreSQL can have multiple databases inside of it. In order to connect to one, you need to run the following command

\c <database_name>

Viewing the tables

To check all the tables that exist inside a database, run

\d

To check the details of a particular table, run

\d+ <table_name>

Exiting the container

To exit the container, run the following command

\q