Goal is to migrate a database from Heroku to Google Cloud
- recreate extensions
- skip copying postgis spatial ref table
- $DB_URI: your full connection string for the heroku database
- $PWD: is the new database password
- $PG: is the new database instance name
- In google cloud console:
- Create a new postgres database instance
- name it
- store as $PG
- give it a password
- store as $PWD
pg_dump -Fc \
--format=plain \
--no-owner --no-acl \
--quote-all-identifiers \
-T table --exclude-table=spatial_ref_sys $DB_URI \
| sed -E 's/(DROP|CREATE|COMMENT ON) EXTENSION/-- \1 EXTENSION/g' \
> dump.sql
TEMPGCEMAIL=`gcloud sql instances describe $PG | grep Email | awk '{print $2}'`
gsutil mb gs://restore
gsutil acl ch -u $TEMPGCEMAIL:W gs://restore
gsutil cp dump.sql gs://restore
gsutil acl ch -u $TEMPGCEMAIL:R gs://restore/dump.sql
- in the gcloud console for the new instance
- Click 'allow only ssl connections'
- Create a client certificate with a unique id of your choice
- download the .pem files
- move .pem files to your working dir
- reduce permission:
chmod 0400 client-key.pem
TEMPGCIP=`gcloud sql instances describe $PG | grep -w ipAddress | awk '{print $3}'`
gcloud sql instances patch $PG --authorized-networks=$TEMPGCIP
Wait a minute or 2...
psql "sslmode=verify-ca sslrootcert=server-ca.pem sslcert=client-cert.pem sslkey=client-key.pem hostaddr=$TEMPGCIP port=5432 user=postgres dbname=postgres password=$PWD"
CREATE EXTENSION IF NOT EXISTS "citext" WITH SCHEMA "public";
CREATE EXTENSION IF NOT EXISTS "hstore" WITH SCHEMA "public";
CREATE EXTENSION IF NOT EXISTS "pg_stat_statements" WITH SCHEMA "public";
CREATE EXTENSION IF NOT EXISTS "postgis" WITH SCHEMA "public";
gcloud sql import sql $PG gs://restore/dump.sql --database=postgres
gsutil rm -r gs://restore