Skip to content

Instantly share code, notes, and snippets.

@keyro90
Created September 2, 2021 12:19
Show Gist options
  • Save keyro90/89dfc0212763b5b224d8f990367749f6 to your computer and use it in GitHub Desktop.
Save keyro90/89dfc0212763b5b224d8f990367749f6 to your computer and use it in GitHub Desktop.
Postgresql dump with docker container script
#!/usr/bin/env bash
HOST=""
PORT=""
DB=""
USER=""
PASSWORD=""
PGSQL_VERSION=""
mkdir -p tmp
rm -rf tmp/*
# create pgpass.
echo "${HOST}:${PORT}:${DB}:${USER}:${PASSWORD}" > ./tmp/pgpass
# set correct permssions.
chmod 600 ./tmp/pgpass
docker kill pgsql-dump >& /dev/null
docker run -e POSTGRES_PASSWORD="password" -e PGPASSFILE="/tmp/pgpass" -v $(pwd)/tmp:/tmp/ --rm -it --name pgsql-dump -d postgres:"${PGSQL_VERSION}"
# need to rewrite parameters inside dump command and generate sql file.
docker exec -it pgsql-dump /usr/bin/pg_dump --host="${HOST}" --port="${PORT}" --username="${USER}" --format=p --encoding=UTF-8 -n public "${DB}" > dump.sql
docker kill pgsql-dump >& /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment