ALTER DATABASE on the current db without an explicit db name?

I would like to write a sql query that modifies the database I'm currently logged into.

Example:

$ psql my_db psql(9.1.1) my_db=> ALTER DATABASE my_db SET some_variable = '0'; ^^^^^ 

Is there a way to avoid choosing a database name in this query?

+7
source share
2 answers

Can't check it right now, but since you're at 9.1, you can try:

 DO $$ BEGIN execute 'alter database '||current_database()||' set some_var = ''0'''; END; $$ 

You may need to select current_database () in a variable to make it work.

+15
source

If you are running the script in psql , you can use the psql lookup mechanism:

 alter database :DBNAME SET ... 

The documentation is here: http://www.postgresql.org/docs/current/interactive/app-psql.html#APP-PSQL-VARIABLES

+7
source

All Articles