Basic command to run with Postgress Container

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

1
docker exec -it postgres psql -Upostgress

Create user and database with all privileges entering the container

1
2
3
4
5
6
7
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
1
docker exec -it postgres psql -U platops
  • Create a database
1
docker exec -it postgres psql -U platops -c "create database platops"
  • Run Select Query
1
docker exec -it postgres psql -Upostgres -a platops -c 'SELECT * FROM posts;'

Import or Export with psql CLI

  • Backup Database
1
docker exec -t postgres pg_dumpall -c -U platops > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql
  • Restore Databases
1
cat dump.sql | docker exec -i postgres psql -U postgres

comments powered by Disqus