Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Install appropriate postgres binaries
- sudo amazon-linux-extras install postgresql9.6
- # rm old files
- rm dump.sql
- rm in.sql
- rm db.import.log
- # Create proper roles/grants
- psql --host=TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
- --command="CREATE ROLE newdb_and_username WITH PASSWORD 'newdb_and_username-fancy-password' login;"
- psql --host TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
- --command="GRANT newdb_and_username to jeffm;
- psql --host TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
- --command="ALTER USER newdb_and_username CREATEDB;"
- # Dump the source DB
- pg_dump --host=ORIGIN.eu-central-1.rds.amazonaws.com --username=olddbname \
- --file=dump.sql --format=c --no-owner --create --clean olddbname
- dropdb --host=TARGET.eu-central-1.rds.amazonaws.com --username jeffm newdb_and_username
- createdb -U jeffm -h TARGET.eu-central-1.rds.amazonaws.com -E UTF-8 -O newdb_and_username olddbname
- # Restore to the target DB
- pg_restore --host=TARGET.eu-central-1.rds.amazonaws.com --username=newdb_and_username \
- --exit-on-error --file in.sql --create --clean --no-owner --no-privileges dump.sql
- # Load via psql
- cat in.sql | psql --host=TARGET.eu-central-1.rds.amazonaws.com --username=newdb_and_username --dbname=postgres >> db.import.log 2>&1
- # rename db
- psql --host=TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
- --command="ALTER DATABASE olddbname RENAME TO newdb_and_username;"
- # revoke perms to createdb
- psql --host TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
- --command="ALTER USER newdb_and_username NOCREATEDB;"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement