How to get the name of the current database from within PostgreSQL?
\c <database_name>in PostgreSQL will connect to the named database.
How can the name of the current database be determined?
ERROR: syntax error at or near "current_database" LINE 1: current_database();
current_database()returns the name of the 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
CALLstatement like some other SQL engines, so you just use
SELECTto call a function.
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.