Search code examples
authenticationdb2-luwdbeaver

How to automatically run some command after connect in DBeaver?


In DBeaver 22.1.2 I created connection to Db2 v11.5.7.0 database with user1.

Every time to start working with connection I do:

  1. Right click on connection and select SQL Editor | New SQL script.
  2. Then I need to switch user with command: set session_user=user2 and then I execute command.
  3. Now running SQLs as usual.

How to do above three steps automatically after I open new script window (or after connect)?

What I have tried so far?

  • If used right click on connection and Edit Connection there is Connection Settings | Shell Commands, but it looks like those commands are run in separate process and also I can't figure it out how to run above command in it.

Solution

  • This answer specifically addresses only setting of the special register SESSION_USER following connection. For more general SQL to be run after connecting, a different answer is necessary.

    Dbeaver 22.1.2 will allow usage of the specialRegisters jdbc property, which lets you assign SESSION_USER special register, and any other special registers you might need.

    Tested with Dbeaver 22.1.2 on Linux, and Db2-LUW 11.5.7.0 on Linux.

    The key detail is that the specialRegisters property cannot be specified currently on the Edit Connection > Driver Properties > Advanced driver properties screen. If you try to add the setting here, dbeaver will silently discard the setting .

    Instead, to get it to work, it's necessary to append to the database-name field on the Edit connection > Main screen, a colon and the setting and a semicolon. For example suppose your database name is sample, then you would use this value in the database field:

    sample:specialRegisters=SESSION_USER=user2;

    This works with Db2-LUW as long as the connecting userid has the SETSESSIONUSER privilege for the target authid (which has to be granted in advance by a suitable user).