Skip to content

Instantly share code, notes, and snippets.

@matti
Created February 17, 2024 15:35
Show Gist options
  • Save matti/0a6cd86f7c8596525ecb2317282cec13 to your computer and use it in GitHub Desktop.
Save matti/0a6cd86f7c8596525ecb2317282cec13 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
export PAGER=""
TARGET_DBNAME=$1
psql --dbname "$TARGET_DBNAME" -c "SELECT * FROM pg_stat_activity;"
#TODO: user hardcoded to postgres
psql --dbname "$TARGET_DBNAME" -c """ \
SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = 'postgres' AND pid <> pg_backend_pid();
"""
psql --dbname "$TARGET_DBNAME" -c "DROP SCHEMA public CASCADE;"
psql --dbname "$TARGET_DBNAME" -c "CREATE SCHEMA public"
psql --dbname "$TARGET_DBNAME" -c "DROP FUNCTION pganalyze.get_column_stats;"
psql --dbname "$TARGET_DBNAME" -c "DROP FUNCTION pganalyze.get_stat_replication;"
psql --dbname "$TARGET_DBNAME" -c "DROP FUNCTION pganalyze.reset_stat_statements;"
psql --dbname "$TARGET_DBNAME" -c "DROP SCHEMA pganalyze;"
pg_dump --format=custom --no-owner --no-acl -Z 9 --dbname "$DATABASE_URL" | pv | pg_restore --format=custom -j1 --no-owner --dbname "$TARGET_DBNAME"
echo ""
echo "TOOK: ${SECONDS}s"
echo "DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment