0% found this document useful (0 votes)
72 views

Postgres SQL Commands in Docker

To connect to a database container, execute docker exec to access the container's bash shell. From there, use psql to connect to Postgres as the postgres user and switch to or create databases. Various commands like \l, \dt, and \du can be used to view database, table, and user information. Type ctrl+d or exit to leave the container bash shell.

Uploaded by

Akhil Sai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Postgres SQL Commands in Docker

To connect to a database container, execute docker exec to access the container's bash shell. From there, use psql to connect to Postgres as the postgres user and switch to or create databases. Various commands like \l, \dt, and \du can be used to view database, table, and user information. Type ctrl+d or exit to leave the container bash shell.

Uploaded by

Akhil Sai
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

To connect to db container bash:

================================
docker exec -it systems-puzzle-master_db_1 bash

Now you are ‘inside’ your container. We can access postgres and create the
database.

connect to postgres sql:


========================
psql -h localhost -p 5432 -U postgres -W
password: postgres

To switch DB:
=============
\c dbname (eg:flaskapp_db)

show list of DB:


================
\l

Quit from db connection


========================
\q (quit)

show list of tables:


=====================
\dt (to show tables)

To exit from container bash:


============================
ctrl + d or exit

create a role/user in postgres sql db:


======================================
createuser --interactive --pwprompt

To show list of user:


=====================
\du (to show list of all users)

to connect to user:dbuser dbname:webserver


==========================================
psql -h localhost -p 5432 --username dbuser --dbname webserver -W

You might also like