79

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();
  • 7
    select current_database() – Mihai Feb 5 '14 at 21:22
  • 1
    Add a SELECT before. – Mihai Feb 5 '14 at 21:29
  • 4
    You know that the prompt my_db already tells you the current database? – a_horse_with_no_name Feb 5 '14 at 21:55
127

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.

32

you can use "\conninfo" in psql

  • From psql version 9.1. That should cover basically all the psql clients out there these days, though. – dezso Oct 8 '14 at 11:42
26
\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.

10
SELECT * FROM current_catalog;
-- and
SELECT current_catalog;

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.