Skip to content

Instantly share code, notes, and snippets.

@jancel
Created January 12, 2024 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jancel/bb3e4eaf85ea39a2cf6aadfa12af84fe to your computer and use it in GitHub Desktop.
Save jancel/bb3e4eaf85ea39a2cf6aadfa12af84fe to your computer and use it in GitHub Desktop.
Short script that will copy a postgres database to somewhere given URLs (Can change username and database name)
#!/bin/bash
: "${SOURCE:?Please provide a SOURCE which should be a database url}"
: "${TARGET:?Please provide a TARGET which should be a database url}"
dump_path=dump
if [ -d "$dump_path" ]; then
echo "/$dump_path already exists. Please remove ./$dump_path before running this script."
exit 1
fi
echo "Dumping database from $SOURCE to $TARGET via $dump_path"
pg_dump \
--clean --if-exists \
--file ${dump_path} \
--format=directory \
--jobs 5 \
--no-acl \
--no-owner \
${SOURCE}
echo "Restoring database from $dump_path to $TARGET"
pg_restore \
--clean --if-exists \
--dbname=${TARGET} \
--format=directory \
--jobs=5 \
--no-acl \
--no-owner \
$dump_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment