Basic command to run with Postgress Container
Connect to the database itself
If we want to intect with our database we need to interact with postgres database first
docker exec -it postgres psql -Upostgress
Create user and database with all privileges entering the container
docker exec -it postgres bash
psql -Upostgress
CREATE USER platops WITH PASSWORD 'platops';
CREATE DATABASE platopsdb;
GRANT ALL PRIVILEGES ON DATABASE platopsdb TO platops;
Run psql commands without entering the container
- To run
psql
docker exec -it postgres psql -U platops
- Create a database
docker exec -it postgres psql -U platops -c "create database platops"
- Run Select Query
docker exec -it postgres psql -Upostgres -a platops -c 'SELECT * FROM posts;'
Import or Export with psql CLI
- Backup Database
docker exec -t postgres pg_dumpall -c -U platops > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql
- Restore Databases
cat dump.sql | docker exec -i postgres psql -U postgres