How to switch databases in psql? – Dev

The best answers to the question “How to switch databases in psql?” in the category Dev.

QUESTION:

In MySQL, I used use database_name;

What’s the psql equivalent?

ANSWER:

You can connect to a database with \c <database> or \connect <database>.

ANSWER:

In PostgreSQL, you can use the \connect meta-command of the client tool psql:

\connect DBNAME

or in short:

\c DBNAME

ANSWER:

You can select the database when connecting with psql. This is handy when using it from a script:

sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test

ANSWER:

At the PSQL prompt, you can do:

\connect (or \c) dbname