1471

In MySQL, I used use database_name;

What's the psql equivalent?

Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • 2
    OK, so this is about `psql`, the front-end for PostgreSQL? – Peter Mortensen Dec 03 '19 at 22:41
  • 3
    A MySQL "database" is in fact a schema. Therefor in most cases, MySQL's "databases" would better be mapped to schemas in Postgres anyway. And if that is done, you can change the current schema using `set schema 'schema_name';` or `set search_path to schema_name;` –  Jan 18 '21 at 07:33
  • schemas are generally under databases. – mckenzm Jun 15 '22 at 02:30

15 Answers15

2222

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

\connect DBNAME

or in short:

\c DBNAME
Erwin Brandstetter
  • 605,456
  • 145
  • 1,078
  • 1,228
Will Hartung
  • 115,893
  • 19
  • 128
  • 203
  • 167
    +1: This is JUST a psql command, in Postgres itself, there is no way to "switch". In fact, psql isn't "switching" in the MySQL context, just closing one connection and opening another. – rfusca Oct 17 '10 at 16:50
  • 17
    So there is no chance to do it with SQL? – Borys Jan 04 '13 at 09:03
  • 11
    So can this work among SQL statements in a `.sql` file? e.g. can I have `CREATE DATABASE mydb;` followed by `\connect mydb`? – J86 Jun 11 '18 at 21:10
  • 3
    @Ciwan I'm pretty sure you can't include `psql` commands in a SQL script file. – Kenny Evitt Aug 07 '18 at 20:51
  • 4
    For "switching" schema, use [`set searchpath=schema_name`](https://www.postgresql.org/docs/12/ddl-schemas.html#DDL-SCHEMAS-PATH). – Hans Ginzel Jun 10 '20 at 08:00
  • 3
    @HansGinzel that changes schema not database there is a huge difference! – Peter Dec 11 '20 at 12:48
  • 2
    @Peter: MySQL's databases are in fact schemas. So in most cases they should be mapped to a schema in Postgres, rather than a database. –  Jan 18 '21 at 07:34
  • @a_horse_with_no_name while that might be true for mysql its not for postgres. – Peter Jan 18 '21 at 07:37
  • @Peter: I know that. However the question is about the MySQL equivalent of switching "a database". –  Jan 18 '21 at 07:38
  • 1
    to connect with username and password `\connect mydb myusername;` – prayagupa Jun 23 '21 at 22:28
  • 3
    @J86 Yes, you can use a `CREATE DATABASE mydb;` line followed by a `\connect mydb` line in a `.sql` file, so long as that file is being run by `psql`. (eg. using psql to run: `\i path_to_sql_script.sql`) [I know because I just tried this and it works] – Venryx Nov 21 '22 at 04:02
198

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

Asclepius
  • 57,944
  • 17
  • 167
  • 143
user229044
  • 232,980
  • 40
  • 330
  • 338
108

At the PSQL prompt, you can do:

\connect (or \c) dbname
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
41

use \c databaseName or \connect databaseName

(Working on psql 13.3)

Anis KCHAOU
  • 830
  • 1
  • 11
  • 11
39

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
Manel Clos
  • 1,785
  • 2
  • 19
  • 15
20

Though not explicitly stated in the question, the purpose is to connect to a specific schema/database.

Another option is to directly connect to the schema. Example:

sudo -u postgres psql -d my_database_name

Source from man psql:

-d dbname
--dbname=dbname
   Specifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line.

   If this parameter contains an = sign or starts with a valid URI prefix (postgresql:// or postgres://), it is treated as a conninfo string. See Section 31.1.1, “Connection Strings”, in the
   documentation for more information.
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
18

\l for databases \c DatabaseName to switch to db \df for procedures stored in particular database

Ammy
  • 369
  • 2
  • 8
14

Using psql's meta-command \c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo (see documentation).

Example: \c MyDatabase

Note that the \c and \connect meta-commands are case-sensitive.

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
11

Use below statement to switch to different databases residing inside your postgreSQL RDMS

\c databaseName
MediaVince
  • 479
  • 1
  • 8
  • 13
Bilal Mahmood
  • 169
  • 1
  • 3
9

You can also connect to a database with a different ROLE as follows.

\connect DBNAME ROLENAME;

or

\c DBNAME ROLENAME;
Abhishek
  • 3,304
  • 4
  • 30
  • 44
7

You can connect using

\c dbname

If you would like to see all possible commands for POSTGRESQL or SQL follow this steps :

  1. rails dbconsole (You will be redirected to your current ENV database)

  2. ? (For POSTGRESQL commands)

or

  1. \h (For SQL commands)

  2. Press Q to Exit

Lakhani Aliraza
  • 435
  • 6
  • 8
5

If you want to switch to a specific database on startup, try

/Applications/Postgres.app/Contents/Versions/9.5/bin/psql vigneshdb;

By default, Postgres runs on the port 5432. If it runs on another, make sure to pass the port in the command line.

/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p2345 vigneshdb;

By a simple alias, we can make it handy.

Create an alias in your .bashrc or .bash_profile

function psql()
{
    db=vigneshdb
    if [ "$1" != ""]; then
            db=$1
    fi
    /Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p5432 $1
}

Run psql in command line, it will switch to default database; psql anotherdb, it will switch to the db with the name in argument, on startup.

Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42
4

Listing and Switching Databases in PostgreSQL When you need to change between databases, you’ll use the \connect command, or \c followed by the database name as shown below:

postgres=# \connect database_name
postgres=# \c database_name

Check the database you are currently connected to.

SELECT current_database();

PostgreSQL List Databases

postgres=# \l
 postgres=# \list
santosh tiwary
  • 598
  • 6
  • 4
2
  Connect to database:

  Method 1 : enter to db : sudo -u postgres psql

  Connect to db : \c dbname

  Method 2 : directly connect to db : sudo -u postgres psql -d my_database_name
-13

You can just enter use [dbName] to switch between databases without reentering your password.

vvvvv
  • 25,404
  • 19
  • 49
  • 81