Repository: dbt-labs/dbt-core
Pull request: Set application_name for Postgres connections
resolves #885



### Description

The `application_name` parameter can now be specified as part of the properties in a postgres target in `profiles.yml` and it will show in `pg_stat_activity`.

### Checklist
 - [x] I have signed the [CLA](https://docs.getdbt.com/docs/contributor-license-agreements)
 - [x] I have run this code in development and it appears to resolve the stated issue
 - [x] This PR includes tests, or tests are not required/relevant for this PR
 - [x] I have updated the `CHANGELOG.md` and added information about my change to the "dbt next" section.


Problem statement:
Set application_name for Postgres connections
When creating a Postgres connection it's possible to set the "application_name" option on the connection, so that the connection is easily identifiable on the server's side. This would make the DB management easier for us.

Also, it'd be great if the name was changeable, so that we can identify different DBT tasks.

Hints from issue discussion:

Commit message: Set application_name for Postgres connections

Patch to review:
diff --git a/plugins/postgres/dbt/adapters/postgres/connections.py b/plugins/postgres/dbt/adapters/postgres/connections.py
index 96bb0b0f5d2..225fdb1d0f3 100644
--- a/plugins/postgres/dbt/adapters/postgres/connections.py
+++ b/plugins/postgres/dbt/adapters/postgres/connections.py
@@ -23,6 +23,7 @@ class PostgresCredentials(Credentials):
     search_path: Optional[str] = None
     keepalives_idle: int = 0  # 0 means to use the default value
     sslmode: Optional[str] = None
+    application_name: Optional[str] = None
 
     _ALIASES = {
         'dbname': 'database',
@@ -93,6 +94,9 @@ def open(cls, connection):
         if credentials.sslmode:
             kwargs['sslmode'] = credentials.sslmode
 
+        if credentials.application_name:
+            kwargs['application_name'] = credentials.application_name
+
         try:
             handle = psycopg2.connect(
                 dbname=credentials.database,


Code review comments:
diff_hunk: @@ -23,6 +23,7 @@ class PostgresCredentials(Credentials):
     search_path: Optional[str] = None
     keepalives_idle: int = 0  # 0 means to use the default value
     sslmode: Optional[str] = None
+    application_name: Optional[str] = None
path: plugins/postgres/dbt/adapters/postgres/connections.py
text: @user1:
What do you think about setting this to `'dbt'` by default? That's what we do on Snowflake and BigQuery today:

https://github.com/fishtown-analytics/dbt/blob/77c10713a325d2bee91d1822951ce5d91ccc3278/plugins/snowflake/dbt/adapters/snowflake/connections.py#L212

https://github.com/fishtown-analytics/dbt/blob/77c10713a325d2bee91d1822951ce5d91ccc3278/plugins/bigquery/dbt/adapters/bigquery/connections.py#L250

@author:
> What do you think about setting this to `'dbt'` by default? That's what we do on Snowflake and BigQuery today:
> 
> https://github.com/fishtown-analytics/dbt/blob/77c10713a325d2bee91d1822951ce5d91ccc3278/plugins/snowflake/dbt/adapters/snowflake/connections.py#L212
> 
> https://github.com/fishtown-analytics/dbt/blob/77c10713a325d2bee91d1822951ce5d91ccc3278/plugins/bigquery/dbt/adapters/bigquery/connections.py#L250

Oh yeah, thats perfect. I can make the change.