Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set application name in golang lib/pq postgresql driver?

Tags:

postgresql

go

I'm writing a golang application and using the golang postgres driver - https://github.com/lib/pq/

I use a connection string like this

'name:pass@host:port/dbname'

I try to add aplication_name param in conn string, but this doesn't work

'name:pass@host:port/dbname?application_name=myapp'

Is it possible to set the application name from golang? (standard way)

like image 740
kabiev Avatar asked Dec 10 '22 12:12

kabiev


2 Answers

Even though it's not mentioned in the documentation, if you look at the lib/pq source code you will find that application_name is supported.

This style connection works as desired:

connstring := fmt.Sprintf("user='%s' password='%s' dbname='%s' host='%s' application_name='%s'", user, password, dbname, host, application_name)
db, err := sql.Open("postgres", connstring)
like image 84
Nugget Avatar answered Mar 07 '23 22:03

Nugget


If you look to the documentation a application_name option is not suuported. Maybe you could use:

fallback_application_name - An application_name to fall back to if one isn't provided.

name:pass@host:port/dbname?fallback_application_name=myapp
like image 31
apxp Avatar answered Mar 07 '23 22:03

apxp