Backup a Database
[root@avcentos ~]# cat pgbackup.sh
cn=postgres # set container name
db=${1:-mydb}
docker exec -t $cn pg_dump -c -U postgres $db > dump_`date +%d-%m-%Y"-"%H%M`.sql
Restore a Database
[root@avcentos ~]# cat pgrestore.sh
if [ ! -f "$1" ]
then
echo File does not exist.
else
cn=postgres # set container name
db=${2:-mydb}
echo Restore to $db...
cat $1 | docker exec -i $cn psql -U postgres -d $db
fi
Drop a Database
# First kill connected sessions
SELECT pg_terminate_backend(pg_stat_activity.pid)
FROM pg_stat_activity
WHERE pg_stat_activity.datname = 'mydb';
# Drop your database
DROP DATABASE mydb;