Is it possible to rename a Cloud SQL database using the gcloud CLI? - google-cloud-sql

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.

Related

Azure Microsoft.DBforPostgreSQL flexibleServers database collations

I tried to create new database with different (than en_US.utf8) collation on Azure Microsoft.DBforPostgreSQL flexibleServers. CLI and Bicep gives "Internal Server Error" as a message and from Portal I cannot change the collation select input at all?
Is it really so that flexibleServers allow only en_US.utf8 collation or is there some properties that I am missing?
I tried creating azure database for PostgreSQL in Azure portal and I have not got the option to select the collation. Even when I tried to create new database, I cannot change character set and collation in portal.
Therefore, I tried to create a new database using SQL command in Azure Data Studio.
Connect postgreSQL server in Azure Data Studio. (Refer the MS document on Connect and query PostgreSQL using Azure Data Studio )
Run this query to list the supported collation and encoding.
select * from pg_collation
Once connected, enter the following SQL script to create a new database with one of the supported collations from the above query.
CREATE DATABASE testdb ENCODING='UTF8' LC_COLLATE='aa_DJ.utf8' TEMPLATE='template0';
To check the collation of newly created Database, use the following query.
select datname,datcollate from pg_database;

Cloud sql postgres DB create Cross project replication

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?

Can't create new schema in an OVH postgres

I have a PostgreSQL server on OVH's Cloud DB and have been using its databases for my web apps.
So far so good.
I got a project where It's a requirement to have schemas. Strangely enough, I am unable to create schemas on the user with "Administrator" privileges.
I have prepared scripts using schemas, so I just need to run them on a prepared database but I need a database with schemas to run them.
Here is my process:
Create a new database
Select option "Create user"
Select option for privilages: "Administrator"
Commit configuration
Wait for database creation
Connect to database with the new config via PGAdmin
Run command create schema if not exists "vMobile";
Recieve following error:
ERROR: permission denied for database my-database-dev
SQL state: 42501
I created a ticket for this but the wait is taking too long.
Support answer
Ok, so I got a response from the OVH support and there is no option for the user to create new schemas as their CloudDB enables access only to schema public and mentioned privileges Administrator, Read/Write, Read, None are only applicable to the public schema.
Workaround
My solution to this is to create tables with schema name included in their names
like so:
Desired outcome: "vCommon"."Route"
Workaround: "public"."vCommon_Route"

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

Insert records to Heroku Postgres database using a rails script

I need to update a dababase from an other database... For this I need to search in existing database if the records exists, if not add... One of the database is locally stored in my computer, and the other one is on heroku... But I don't know how to access heroku database from my computer to create the inserts and to query if I have to insert or not...
Any idea how can I do something like this?
You can connect via any DB tool, e.g. Oracle SQL Developer with Postgres JDBC connector, to a Heroku Postgres DB. The parameters are listed in the GUI of postgres.heroku.com for your instance.