How to Switch Database in psql?

Sometimes we host multiple database in single PostgreSQL instance and we need to switch between them in psql.

TL;DR: SELECT VERSION(); or pg_config –version

When we say version, there are two possibilities, client version and server version.

Checking Client Version

psql is our client on terminals, to check it’s version psql –version command:

postgres@bbe89c90b5eb:~$ psql --version
psql (PostgreSQL) 15.3 (Debian 15.3-1.pgdg110+1)

For this article, i created two databases:

test1 and test2 will be our database to switch between.

Tip of The Day

You can use \l shortcut to list database and database templates in PostgreSQL. Also \list can be used to list same.

Switch Database in psql

That is pretty simple. Like Db2, PostgreSQL uses “connect” terminology. And yes you are right! As you guess it is:

postgres=# \connect test1
You are now connected to database "test1" as user "postgres".
test1=# \connect test2
You are now connected to database "test2" as user "postgres".
test2=#

Like other commands, it has a shortcut \c:

test2=# \c test1
You are now connected to database "test1" as user "postgres".
test1=# \c test2
You are now connected to database "test2" as user "postgres".
test2=#

A new post everyday, subscribe now and don’t miss it!

Subscribe to our newsletter for cool news

Hi! I’m an IT Specialist

I want to hear from you! I am Working with enterprises for 10+ years to improve their infrastructure and efficiency.

Get in touch with me.

Leave a Reply

Discover more from Empower. Innovate. Transform.

Subscribe now to keep reading and get access to the full archive.

Continue reading