I know that PostgreSQL database allows to keep track of application name for each connection and it looks like application_name variable should work with RPostgreSQL, but how exactly should I do this? Adding variable application_name = "test" to dbConnect doesn't work.
2 Answers
I'm not sure you can pass application_name='test' as an argument to dbConnect in RPostgreSQL (there is an optional options argument, but I couldn't figure out what kind of data it expects).
An alternative would be to run the following SQL query immediately after opening the connection:
SET application_name='test'
and it should work until you close the connection.
Comments
It's not well documented but something like this works:
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv,
dbname = "<my_db>",
host = "<my_host>",
[...],
options = "-c application_name=my_app_name"
)
It works with dbPool too (package 'pool'):
pool <- dbPool(
drv = dbDriver("PostgreSQL"),
dbname = "<my_db>",
host = "<my_host>",
[...]
minSize = 0,
maxSize = 3,
options = "-c application_name=my_app_name"
)
Check with pg_stat_activity
mydb=> SELECT state, usename, application_name FROM pg_stat_activity WHERE datname = 'mydb';
state | usename | application_name
--------+----------+------------------
active | postgres | psql
idle | myuser | my_app_name
dbConnect()in github.com/rstats-db/RPostgres