Postgresql – How to grant Connect & Create Privilege on current Database in PostgreSQL

permissionspostgresql

After looking at the docs, I know that you can grant Connect & Create Privileges by using the following commands:

GRANT CREATE ON DATABASE <db_name> TO user;
GRANT CONNECT ON DATABASE <db_name> TO user;

How do I grant these privileges on the current database without specifying it's name?

I'm trying to write a script which will be used to grant all rights whenever a new database is created by my application, and hence I don't know what the name will be while writing the script.

Best Answer

You could use a DO block with dynamic SQL:

do 
$$ 
begin
  execute format('grant create, connect on database %I to %I', current_database(), 'some_user');
end;
$$;