Access PostgreSQL database command line terminal in a container

Access PostgreSQL database command line terminal in a container

sudo docker exec -it [CONTAINER NAME] psql -U postgres

Commands:

\c [DATABASE NAME] ~ connects to a database
\d ~ checks all tables
\d+ [TABLE NAME] ~ checks 
\q ~ quit

You can also run this as a shell script

#!/bin/bash
sudo docker exec -it [CONTAINER NAME] psql -U postgres -c "CREATE DATABASE mydatabase OWNER = postgres TABLESPACE pg_default;"

sudo docker exec -it [CONTAINER NAME] psql -d gblcore -U postgres -c "CREATE SCHEMA myschema AUTHORIZATION postgres;"

sudo docker exec -i [CONTAINER NAME] pg_restore -U postgres -d mydatabase < /tmp/production20180529.dump

Leave a Reply