How to determine current database?

Started by Ron St-Pierreabout 22 years ago5 messagesgeneral
Jump to latest
#1Ron St-Pierre
rstpierre@syscor.com

I am using postgres 7.3.4 and need to be able to determine which
database a query is being run in (from a script). pg_database lists
databases but doesn't tell me which one is currently active. Is there a
query I can use along the lines of:
UPDATE tblUpdates SET xxx=1234 WHERE pg_current = TRUE;
or
UPDATE tblUpdates SET xxx=1234 WHERE pg_current = thisDBname;

We have about 15 databases all set up identically and when the structure
changes I run scripts to update them to ensure that they are all the
same. I need to create two new databases which have slight changes
(different default values - for now) and want to be able to have my
scripts be able to determine which database their running from.

Thanks

Ron

#2Joe Conway
mail@joeconway.com
In reply to: Ron St-Pierre (#1)
Re: How to determine current database?

Ron St-Pierre wrote:

I am using postgres 7.3.4 and need to be able to determine which
database a query is being run in (from a script). pg_database lists
databases but doesn't tell me which one is currently active.

See:
http://www.postgresql.org/docs/7.3/static/functions-misc.html

HTH,

Joe

#3Eric Ridge
ebr@tcdi.com
In reply to: Ron St-Pierre (#1)
Re: How to determine current database?

On Feb 13, 2004, at 6:05 PM, Ron St-Pierre wrote:

I am using postgres 7.3.4 and need to be able to determine which
database a query is being run in (from a script). pg_database lists
databases but doesn't tell me which one is currently active. Is there
a query I can use along the lines of:

The built-in function "current_database()" returns the current database
name.

=# select current_database();
current_database
------------------
testing
(1 row)

Use it in your update statements too.

eric

Show quoted text

UPDATE tblUpdates SET xxx=1234 WHERE pg_current = TRUE;
or
UPDATE tblUpdates SET xxx=1234 WHERE pg_current = thisDBname;

We have about 15 databases all set up identically and when the
structure changes I run scripts to update them to ensure that they are
all the same. I need to create two new databases which have slight
changes (different default values - for now) and want to be able to
have my scripts be able to determine which database their running
from.

Thanks

Ron

---------------------------(end of
broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to
majordomo@postgresql.org

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Ron St-Pierre (#1)
Re: How to determine current database?

Ron St-Pierre <rstpierre@syscor.com> writes:

I am using postgres 7.3.4 and need to be able to determine which
database a query is being run in (from a script).

See current_database():
http://www.postgresql.org/docs/7.4/static/functions-misc.html

regards, tom lane

#5Ron St-Pierre
rstpierre@syscor.com
In reply to: Eric Ridge (#3)
Re: How to determine current database?

Eric Ridge wrote:

On Feb 13, 2004, at 6:05 PM, Ron St-Pierre wrote:

I am using postgres 7.3.4 and need to be able to determine which
database a query is being run in (from a script). pg_database lists
databases but doesn't tell me which one is currently active. Is there
a query I can use along the lines of:

The built-in function "current_database()" returns the current
database name.

=# select current_database();
current_database
------------------
testing
(1 row)

Use it in your update statements too.

eric

That's exactly what I need!

Joe Conway wrote:

See:
http://www.postgresql.org/docs/7.3/static/functions-misc.html

Thanks guys. I was searching the docs looking for functions starting
with pg_

Ron