Skip to contents

Registers a function that returns the password for each new PostgreSQL connection. Use it for a password that expires, such as an Entra access token.

Usage

csdb_set_password_hook(hook)

Arguments

hook

A function with no arguments that returns the password, or NULL to clear the hook.

Value

Invisibly returns the previous hook, or NULL when no hook was set.

Details

csdb calls the hook with no arguments, once for each attempt of DBConnection_v9$connect() with the driver "PostgreSQL Unicode". It uses the current return value as the password. A PostgreSQL load uses the connection, and calls the hook only when it opens a new one.

The hook MUST return a single non-empty string. Any other value stops the connection with an error that names the hook. The error does not show the value.

The hook applies to PostgreSQL only. A SQL Server, SQLite or other connection, and a bcp load, use the password in their settings. csdb therefore never sends the hook value to another kind of server.

The message of a failed connection shows *** in place of the hook value.

See also

csdb_set_auth_hook registers a function that DBConnection_v9$connect() calls after its first failed attempt.

Other password hook functions: csdb_get_password_hook()

Examples

# The hook is held in the csdb.password_hook option. Registering the hook
# does not call it.
previous <- csdb_set_password_hook(function() "example-token")
is.function(csdb_get_password_hook())
#> [1] TRUE

# Put back whatever was registered before.
csdb_set_password_hook(previous)
csdb_get_password_hook()
#> NULL