Reputation: 389
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)
Upvotes: 5
Views: 4496
Reputation: 81
For anyone looking into this in 2023, if you are using kubernetes or any other cloud native environment the pq library also supports setting the application name via ENV variable by simply specifying:
export PGAPPNAME=myapp
You can verify this by checking the connection stats:
SELECT * FROM pg_stat_activity ORDER BY application_name;
Reference: https://github.com/lib/pq/blob/v1.10.9/conn.go#L2045
Upvotes: 1
Reputation: 7593
You can set the application name one of two ways.
// Application name as space-separated list of options.
sql.Open("postgres", "user=myuser password=mypass host=localhost port=5432 dbname=mydb sslmode=disable application_name=myapp")
or
// Application name as query param.
sql.Open("postgres", "postgres://myuser:mypass@localhost:5432/mydb?sslmode=disable&application_name=myapp")
Upvotes: 1
Reputation: 146
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)
Upvotes: 11
Reputation: 5914
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
Upvotes: 6