Access PostgreSQL database command line terminal in a container

Access PostgreSQL database command line terminal in a container

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

Commands:

1
2
3
4
\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

1
2
3
4
5
6
#!/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