200

Using \c <database_name> in PostgreSQL will connect to the named database.

How can the name of the current database be determined?

Entering:

my_db> current_database();

produces:

ERROR:  syntax error at or near "current_database"
LINE 1: current_database();
Alex Willison
  • 199
  • 10
Amelio Vazquez-Reina
  • 2,125
  • 2
  • 14
  • 8

5 Answers5

298

The function current_database() returns the name of the current database:

 SELECT current_database();

It's an SQL function, so you must call it as part of an SQL statement. PostgreSQL doesn't support running functions as standalone queries, and has no CALL statement like some other SQL engines, so you just use SELECT to call a function.

Erwin Brandstetter
  • 175,982
  • 27
  • 439
  • 600
Craig Ringer
  • 56,343
  • 5
  • 158
  • 190
62

you can use "\conninfo" in psql

John Duddy
  • 721
  • 5
  • 2
41
\c

prints something like

You are now connected to database "foobar" as user "squanderer".

Use this if you don't mind creating a new connection, because this is what happens. The \connect (shortened as \c) without all parameters will create a new connection identical to your current one. The current connection is closed.

See the \connect command spec on http://www.postgresql.org/docs/9.3/static/app-psql.html :

If any of dbname, username, host or port are omitted (...) , the value of that parameter from the previous connection is used.

András Váczi
  • 31,278
  • 13
  • 101
  • 147
shful
  • 511
  • 4
  • 2
17
SELECT * FROM current_catalog;
-- and
SELECT current_catalog;

...both work as well (catalog is standard SQL for database)

Peter Vandivier
  • 4,626
  • 1
  • 23
  • 45
nikooters
  • 171
  • 1
  • 2
3

PostgreSQL List Databases - To list all the databases created within PostgreSQL Server.

postgres=# \l
postgres=# \list

To Check Current database you are connected to.

SELECT current_database();