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?
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.
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.
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.
If you're on 9.1 you can use:
DO $$
BEGIN
execute 'alter database '||current_database()||' set some_var = ''0''';
END
$$;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With