Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. # Install appropriate postgres binaries
  2. sudo amazon-linux-extras install postgresql9.6
  3.  
  4. # rm old files
  5. rm dump.sql
  6. rm in.sql
  7. rm db.import.log
  8.  
  9. # Create proper roles/grants
  10. psql --host=TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
  11. --command="CREATE ROLE newdb_and_username WITH PASSWORD 'newdb_and_username-fancy-password' login;"
  12. psql --host TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
  13. --command="GRANT newdb_and_username to jeffm;
  14. psql --host TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
  15. --command="ALTER USER newdb_and_username CREATEDB;"
  16.  
  17. # Dump the source DB
  18. pg_dump --host=ORIGIN.eu-central-1.rds.amazonaws.com --username=olddbname \
  19. --file=dump.sql --format=c --no-owner --create --clean olddbname
  20.  
  21. dropdb --host=TARGET.eu-central-1.rds.amazonaws.com --username jeffm newdb_and_username
  22. createdb -U jeffm -h TARGET.eu-central-1.rds.amazonaws.com -E UTF-8 -O newdb_and_username olddbname
  23.  
  24. # Restore to the target DB
  25. pg_restore --host=TARGET.eu-central-1.rds.amazonaws.com --username=newdb_and_username \
  26. --exit-on-error --file in.sql --create --clean --no-owner --no-privileges dump.sql
  27.  
  28. # Load via psql
  29. cat in.sql | psql --host=TARGET.eu-central-1.rds.amazonaws.com --username=newdb_and_username --dbname=postgres >> db.import.log 2>&1
  30.  
  31. # rename db
  32. psql --host=TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
  33. --command="ALTER DATABASE olddbname RENAME TO newdb_and_username;"
  34.  
  35. # revoke perms to createdb
  36. psql --host TARGET.eu-central-1.rds.amazonaws.com --username=jeffm --dbname=postgres \
  37. --command="ALTER USER newdb_and_username NOCREATEDB;"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement