Cloud sql postgres DB create Cross project replication - postgresql

In the Gcloud SQL Postgres database, I am currently using gcloud export and import command to create database backup in a different project. which takes a long time.
As gcloud allows us to create read replica but it creates with in the same project.
my question is?
how can i create a replica of cloud sql postgres instance in another project postgres instacne?

Related

Is it possible to rename a Cloud SQL database using the gcloud CLI?

I would like to rename a Cloud SQL database using only the gcloud CLI.
I can see that I can patch a database using the CLI. However it seems to only patch either the collation or the charset but not the database name itself.
Do you know if there is a way to achieve it with the CLI ?
If not, I will use a Cloud SQL Auth Proxy and run the ALTER command directly on the database.
I don't think, there is an in-built option to rename database using gcloud command-line.
Available option is login to database engine and alter database name using ALTER DATABASE command, if it supports in-place database alteration.
Another option is use gcloud sql to export your database to sql file, make changes to your database name and import it back to Cloud SQL instance.
If you are using Cloud SQL MySQL instance, you can use mysqldump to backup and restore with new database name or use rename table with new database name.

How to create local clone of a GCP Cloud SQL export for PostgresQL

I have exported a database as a SQL file in Google Cloud Platform Cloud SQL Postgres Database using their GUI wizard. I want to import the data from the dump into my local PostgresQL installation. I tried using PgAdmin4 with various settings but kept failing. I then tried the following command which retrieved the data but most of the relationships are gone.
psql -f "SQLFile.sql" --host "localhost" --port "5432" --username "dbusername" "myDBName"
How can I create a local clone of my Cloud SQL db?
Posting an answer for anyone else who needs this. The following was performed on a Windows 10 machine.
Export from Cloud SQL using the GCP console GUI. This will save it to a cloud storage bucket and you can download it from GCS bucket.
On local machine - remove any database with the same name as the one you want to import . Then create a fresh database with the same name as the one you want to import. (in my case this was the issue)
In terminal go to your PostgreSQL bin folder. Example:
cd “C:\Program Files\PostgreSQL\13\bin
Then run the following command - replacing values as per your needs.
psql -f "<full-path-to-downloaded-sql-file>" --host "localhost" --port "5432" --username "<local-postgres-username>" "<db-name-to-import>”
You may need to grant privileges to users on tables. This can be done using the grant wizard in pgAdmin 4 or as described in this answer.

Adding a database in a new cluster created on postgresql?

I have created a database cluster in PostgreSQL-9.6 using pg_createcluster command with user (-u) specified different from default postgres. I would like to know how to add a new/existing database into this cluster instead of the default 'main' cluster?
Conect to the template1 or postgres database in the new cluster and execute sql
CREATE DATABASE etc

Obtain aws cli command to create RDS instance from existing database

Is there a way to automatically generate the database creation string for an empty database with settings from an existing database?
I created an AWS RDS postgresql database instance using the console in my browser. I selected all kind of setting in my browser and launched the instance. Now I want to replicate this database (empty) with a command for programmatic use. I would like to delete the existing RDS instance so a raw command would be preferred (no reference to another RDS) but something like:
aws create-db-instance --db-name database02 --allocated-storage 200 --engine postgres etc.
You'll need to create a snapshot of the source Database, instance or cluster and then create a new database based on that snapshot. You can delete the source database after the snapshot is complete.
aws rds create-db-snapshot
--db-snapshot-identifier mydbsnapshot
--db-instance-identifier mydb
aws rds restore-db-instance-from-db-snapshot
--db-instance-identifier mynewdbinstance
--db-snapshot-identifier mydbsnapshot
http://docs.aws.amazon.com/cli/latest/reference/rds/create-db-snapshot.html
http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_RestoreFromSnapshot.html
http://docs.aws.amazon.com/cli/latest/reference/rds/restore-db-instance-from-db-snapshot.html

Automate backup of a single schema in PostgreSQLl on AWS RDS

Setup --
I have a postgresql db running on AWS RDS. We access it from an EC2 instance using pgAdmin 3.
Current Scenario --
AWS takes a daily backup of the entire RDS instance as RDS snapshots which is stored for 15 days.
I take a manual backup of one important schema (all the data in schema included) using pgAdmin and then push this backup file to s3.
Question --
How do I automate this backup of a single schema (including data) in postgresql and store it on S3 (any application or script). Is there a way to automate it in pgadmin just for one schema and not the entire database.
You need to use a command line tool pg_dump:
pg_dump \
--host=myhostname.qwertyuiop.sc-moon-1.rds.amazonaws.com \
--username=myusername --dbname=mydbname \
--format=custom --file=mydbname.pg_dump
It needs to be the same major version as the database.