abnerh69
abnerh69

Reputation: 654

How can I change database inside Shell on Google Cloud Platform using PostgreSQL (psql)?

On GCP, when you connect to Google Cloud Shell, and then connect to PostgreSQL database instance using "gcloud sql connect..." GCP connects you to postgres database.

How can I change to another database already created?

Is there any psql (or specific GCP Shell) command to change database connection?

Upvotes: 0

Views: 2002

Answers (2)

abnerh69
abnerh69

Reputation: 654

As @scottsargent answer: \c DBNAME is the solution.

You need on GCP Shell to add a semicolon ; at the end, press ENTER and provide the user password to change database connection when psql ask for it:

postgres=> \c elboticario;
Password for user postgres:
psql (9.6.7, server 9.6.6)
SSL connection (protocol: TLSv1.2, cipher: **************-SHA256, bits: 128, compression: off)
You are now connected to database "elboticario" as user "postgres".
elboticario=>

Upvotes: 1

scottsargent
scottsargent

Reputation: 101

I have not tried GCP, so I'm not sure if this works there. But in some of my scripts I've the need to do this.

In those scripts, I use

\c DBNAME

More info: How to switch databases in psql?

Upvotes: 1

Related Questions