0

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?

abnerh69
  • 529
  • 3
  • 9
  • 19

2 Answers2

1

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?

scottsargent
  • 101
  • 5
  • But where should I type that? – abnerh69 Apr 27 '18 at 21:48
  • If you're running it through psql you can put it right in your script. Here's a gist I created that shows how to do it. https://gist.github.com/ssargent/e315cd90ebd9ad9b915f324be71cc41e Just add \c DBNAME; in the script and it changes the database. In this example I connect to my main db, and then change to the two databases i created db1 and db2 – scottsargent Apr 27 '18 at 21:58
1

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=>
TommyBs
  • 9,354
  • 4
  • 34
  • 65
abnerh69
  • 529
  • 3
  • 9
  • 19