220

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

6 Answers 6

328

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.

5
  • 1
    Modern versions of Postgres have added support for CALL. Commented Sep 28, 2021 at 13:52
  • 1
    There's SELECT current_user, the fact that current_database is a function is surprising. Commented Apr 25, 2023 at 4:19
  • 4
    @SteveChavez It's because the SQL standard defines current_user as a magic keyword. Postgres does not want to expand the set of keywords, so anything not specified by the standard tends to be added as functions. Commented May 22, 2023 at 3:35
  • To get that into an ALTER DATABASE .. query, I need dynamic sql or psql vars I reckon. @SteveChavez btw there is current_catalog, see this answer. Commented Oct 22, 2024 at 12:48
  • @Rafs Yes, because IIRC ALTER DATABASE cannot run in a function context. Maybe it can as a PROCEDURE. I haven't checked. Try a PL/PgSQL PROCEDURE with an EXECUTE block. Commented Oct 23, 2024 at 0:07
66

you can use "\conninfo" in psql

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

0
19
SELECT * FROM current_catalog;
-- and
SELECT current_catalog;

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

0
4

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

Using INFORMATION_SCHEMA views:

information_schema_catalog_name

information_schema_catalog_name is a table that always contains one row and one column containing the name of the current database (current catalog, in SQL terminology).

SELECT * FROM INFORMATION_SCHEMA.INFORMATION_SCHEMA_CATALOG_NAME;
-- catalog_name
-- <name>

db<>fiddle demo

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.