HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlCritical

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

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
postgresqlthedatabasewithingetnamecurrenthowfrom

Problem

Using \c 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();

Solution

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.

Code Snippets

SELECT current_database();

Context

StackExchange Database Administrators Q#58312, answer score: 298

Revisions (0)

No revisions yet.