Site icon Learning & Doing

Migrate to Cloud SQL for PostgreSQL using Database Migration Service

SQL

“Migrate to Cloud SQL for PostgreSQL using Database Migration Service”

Daftar Isi

Pengantar

Migrasi database melalui Layanan Migrasi Database memerlukan beberapa persiapan database sumber, termasuk membuat pengguna khusus dengan hak replikasi, menambahkan ekstensi database pglogical ke database sumber, dan juga memberikan hak skemata dan tabel di database yang akan dimigrasikan. sebagai database postgres, kepada pengguna tersebut.

Praktikum

Task 1. Prepare the source database for migration

Upgrade the database with the pglogical extension

sudo apt install postgresql-13-pglogical
sudo su - postgres -c "gsutil cp gs://cloud-training/gsp918/pg_hba_append.conf ."
sudo su - postgres -c "gsutil cp gs://cloud-training/gsp918/postgresql_append.conf ."
sudo su - postgres -c "cat pg_hba_append.conf >> /etc/postgresql/13/main/pg_hba.conf"
sudo su - postgres -c "cat postgresql_append.conf >> /etc/postgresql/13/main/postgresql.conf"
sudo systemctl restart postgresql@13-main
sudo su - postgres
psql
\c postgres;
CREATE EXTENSION pglogical;
\c orders;
CREATE EXTENSION pglogical;
\c gmemegen_db;
CREATE EXTENSION pglogical;
\l

Create the database migration user

CREATE USER migration_admin PASSWORD 'DMS_1s_cool!';
ALTER DATABASE orders OWNER TO migration_admin;
ALTER ROLE migration_admin WITH REPLICATION;

Assign permissions to the migration user

\c postgres;
GRANT USAGE ON SCHEMA pglogical TO migration_admin;
GRANT ALL ON SCHEMA pglogical TO migration_admin;
GRANT SELECT ON pglogical.tables TO migration_admin;
GRANT SELECT ON pglogical.depend TO migration_admin;
GRANT SELECT ON pglogical.local_node TO migration_admin;
GRANT SELECT ON pglogical.local_sync_status TO migration_admin;
GRANT SELECT ON pglogical.node TO migration_admin;
GRANT SELECT ON pglogical.node_interface TO migration_admin;
GRANT SELECT ON pglogical.queue TO migration_admin;
GRANT SELECT ON pglogical.replication_set TO migration_admin;
GRANT SELECT ON pglogical.replication_set_seq TO migration_admin;
GRANT SELECT ON pglogical.replication_set_table TO migration_admin;
GRANT SELECT ON pglogical.sequence_state TO migration_admin;
GRANT SELECT ON pglogical.subscription TO migration_admin;
\c orders;
GRANT USAGE ON SCHEMA pglogical TO migration_admin;
GRANT ALL ON SCHEMA pglogical TO migration_admin;
GRANT SELECT ON pglogical.tables TO migration_admin;
GRANT SELECT ON pglogical.depend TO migration_admin;
GRANT SELECT ON pglogical.local_node TO migration_admin;
GRANT SELECT ON pglogical.local_sync_status TO migration_admin;
GRANT SELECT ON pglogical.node TO migration_admin;
GRANT SELECT ON pglogical.node_interface TO migration_admin;
GRANT SELECT ON pglogical.queue TO migration_admin;
GRANT SELECT ON pglogical.replication_set TO migration_admin;
GRANT SELECT ON pglogical.replication_set_seq TO migration_admin;
GRANT SELECT ON pglogical.replication_set_table TO migration_admin;
GRANT SELECT ON pglogical.sequence_state TO migration_admin;
GRANT SELECT ON pglogical.subscription TO migration_admin;
GRANT USAGE ON SCHEMA public TO migration_admin;
GRANT ALL ON SCHEMA public TO migration_admin;
GRANT SELECT ON public.distribution_centers TO migration_admin;
GRANT SELECT ON public.inventory_items TO migration_admin;
GRANT SELECT ON public.order_items TO migration_admin;
GRANT SELECT ON public.products TO migration_admin;
GRANT SELECT ON public.users TO migration_admin;
\c gmemegen_db;
GRANT USAGE ON SCHEMA pglogical TO migration_admin;
GRANT ALL ON SCHEMA pglogical TO migration_admin;
GRANT SELECT ON pglogical.tables TO migration_admin;
GRANT SELECT ON pglogical.depend TO migration_admin;
GRANT SELECT ON pglogical.local_node TO migration_admin;
GRANT SELECT ON pglogical.local_sync_status TO migration_admin;
GRANT SELECT ON pglogical.node TO migration_admin;
GRANT SELECT ON pglogical.node_interface TO migration_admin;
GRANT SELECT ON pglogical.queue TO migration_admin;
GRANT SELECT ON pglogical.replication_set TO migration_admin;
GRANT SELECT ON pglogical.replication_set_seq TO migration_admin;
GRANT SELECT ON pglogical.replication_set_table TO migration_admin;
GRANT SELECT ON pglogical.sequence_state TO migration_admin;
GRANT SELECT ON pglogical.subscription TO migration_admin;
GRANT USAGE ON SCHEMA public TO migration_admin;
GRANT ALL ON SCHEMA public TO migration_admin;
GRANT SELECT ON public.meme TO migration_admin;
\c orders;
\dt
ALTER TABLE public.distribution_centers OWNER TO migration_admin;
ALTER TABLE public.inventory_items OWNER TO migration_admin;
ALTER TABLE public.order_items OWNER TO migration_admin;
ALTER TABLE public.products OWNER TO migration_admin;
ALTER TABLE public.users OWNER TO migration_admin;
\dt

\q
exit

Task 2. Create a Database Migration Service connection profile for a stand-alone PostgreSQL database

Get the connectivity information for the PostgreSQL source instance

Create a new connection profile for the PostgreSQL source instance

Task 3. Create and start a continuous migration job

Create a new continuous migration job

Migration job name : vm-to-cloudsql
Source database engine : PostgreSQL
Destination region : US-East
Destination database engine : Cloud SQL for PostgreSQL
Migration job type : Continuous

Define the source instance

Create the destination instance

Destination Instance ID : postgresql-cloudsql
Root password : supersecret!
Database version : Cloud SQL for PostgreSQL 13
Instance connectivity : Private IP and Public IP
Use an automatically allocated IP range
Machine type : Pilih Standard dan check 1 vCPU, 3.75 GB
Storage type : SSD
Storage capacity : 10 GB

Define the connectivity method

Connectivity method : VPC peering
VPC : default

Allow access to the posgresql-vm instance from automatically allocated IP range

sudo nano /etc/postgresql/13/main/pg_hba.conf
#GSP918 - allow access to all hosts
host    all all 10.107.176.0/24   md5
sudo systemctl start postgresql@13-main

Test and start the continuous migration job

Task 4. Confirm the data in Cloud SQL for PostgreSQL

Connect to the PostgreSQL instance

gcloud sql connect postgresql-cloudsql --user=postgres --quiet
supersecret!

Review the data in the Cloud SQL for PostgreSQL instance

\c orders;
supersecret!
select * from distribution_centers;

Update stand-alone source data to test continuous migration

export VM_NAME=postgresql-vm
export PROJECT_ID=$(gcloud config list --format 'value(core.project)')
export POSTGRESQL_IP=$(gcloud compute instances describe ${VM_NAME} \
  --zone=(zone) --format="value(networkInterfaces[0].accessConfigs[0].natIP)")
echo $POSTGRESQL_IP
psql -h $POSTGRESQL_IP -p 5432 -d orders -U migration_admin
DMS_1s_cool!
\c orders;
insert into distribution_centers values(-80.1918,25.7617,'Miami FL',11);
\q

Connect to the Cloud SQL PostgreSQL database to check that updated data has been migrated

gcloud sql connect postgresql-cloudsql --user=postgres --quiet
supersecret!

Review data in Cloud SQL for PostgreSQL database

\c orders;
supersecret!
select * from distribution_centers;

Task 5. Promote Cloud SQL to be a stand-alone instance for reading and writing data

Penutup

Sahabat Blog Learning & Doing demikianlah penjelasan mengenai Migrate to Cloud SQL for PostgreSQL using Database Migration Service. Semoga Bermanfaat . Sampai ketemu lagi di postingan berikut nya.

Exit mobile version