Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ALTER DATABASE on a current db without explicit db name?

Tags:

sql

postgresql

I would like to be able to write sql query that changes the database to which I am currently logged in.

Example:

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

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

like image 263
Kuba Avatar asked Feb 17 '12 15:02

Kuba


People also ask

How do I change current database?

Switching between databases is another way of saying you are closing one connection and opening another. When you need to change between databases, you'll use the “connect” command, which is conveniently shortened to \c, followed by the database name.

Which command can be used to change or edit database file?

The alter command is used when we want to modify a database or any object contained in the database. The drop command is used to delete databases from MySQL server or objects within a database.

How do I change my redshift database name?

You can't rename the dev, padb_harvest, template0, or template1 databases, and you can't rename the current database. Only the database owner or a superuser can rename a database; non-superuser owners must also have the CREATEDB privilege. New database name. Changes the owner of the specified database.


1 Answers

If you're on 9.1 you can use:

DO $$
BEGIN
   execute 'alter database '||current_database()||' set some_var = ''0''';
END
$$;
like image 69
a_horse_with_no_name Avatar answered Oct 12 '22 23:10

a_horse_with_no_name