How to get the name of the current database from within PostgreSQL?

All we need is an easy explanation of the problem, so here it is.

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

How can the name of the current database be determined?


my_db> current_database();


ERROR:  syntax error at or near "current_database"
LINE 1: current_database();

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

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.

Method 2

you can use “\conninfo” in psql

Method 3


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 :

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

Method 4

SELECT * FROM current_catalog;
-- and
SELECT current_catalog;

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

Method 5

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();

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply