Repository: dbt-labs/dbt-core

Issue:
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. 


Discussion:

Resolving patch:
diff --git a/plugins/postgres/dbt/adapters/postgres/connections.py b/plugins/postgres/dbt/adapters/postgres/connections.py
--- 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] = 'dbt'
 
     _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,


Test patch:
diff --git a/test/unit/test_postgres_adapter.py b/test/unit/test_postgres_adapter.py
--- a/test/unit/test_postgres_adapter.py
+++ b/test/unit/test_postgres_adapter.py
@@ -119,7 +119,8 @@ def test_default_keepalive(self, psycopg2):
             host='thishostshouldnotexist',
             password='password',
             port=5432,
-            connect_timeout=10)
+            connect_timeout=10,
+            application_name='dbt')
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
     def test_changed_keepalive(self, psycopg2):
@@ -135,8 +136,39 @@ def test_changed_keepalive(self, psycopg2):
             password='password',
             port=5432,
             connect_timeout=10,
-            keepalives_idle=256)
+            keepalives_idle=256,
+            application_name='dbt')
+
+    @mock.patch('dbt.adapters.postgres.connections.psycopg2')
+    def test_default_application_name(self, psycopg2):
+        connection = self.adapter.acquire_connection('dummy')
 
+        psycopg2.connect.assert_not_called()
+        connection.handle
+        psycopg2.connect.assert_called_once_with(
+            dbname='postgres',
+            user='root',
+            host='thishostshouldnotexist',
+            password='password',
+            port=5432,
+            connect_timeout=10,
+            application_name='dbt')
+
+    @mock.patch('dbt.adapters.postgres.connections.psycopg2')
+    def test_changed_application_name(self, psycopg2):
+        self.config.credentials = self.config.credentials.replace(application_name='myapp')
+        connection = self.adapter.acquire_connection('dummy')
+
+        psycopg2.connect.assert_not_called()
+        connection.handle
+        psycopg2.connect.assert_called_once_with(
+            dbname='postgres',
+            user='root',
+            host='thishostshouldnotexist',
+            password='password',
+            port=5432,
+            connect_timeout=10,
+            application_name='myapp')
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
     def test_role(self, psycopg2):
@@ -161,6 +193,7 @@ def test_search_path(self, psycopg2):
             password='password',
             port=5432,
             connect_timeout=10,
+            application_name='dbt',
             options="-c search_path=test")
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
@@ -177,7 +210,8 @@ def test_sslmode(self, psycopg2):
             password='password',
             port=5432,
             connect_timeout=10,
-            sslmode="require")
+            sslmode="require",
+            application_name='dbt')
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
     def test_schema_with_space(self, psycopg2):
@@ -193,6 +227,7 @@ def test_schema_with_space(self, psycopg2):
             password='password',
             port=5432,
             connect_timeout=10,
+            application_name='dbt',
             options="-c search_path=test\ test")
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
@@ -208,7 +243,8 @@ def test_set_zero_keepalive(self, psycopg2):
             host='thishostshouldnotexist',
             password='password',
             port=5432,
-            connect_timeout=10)
+            connect_timeout=10,
+            application_name='dbt')
 
     @mock.patch.object(PostgresAdapter, 'execute_macro')
     @mock.patch.object(PostgresAdapter, '_get_catalog_schemas')
diff --git a/test/unit/test_redshift_adapter.py b/test/unit/test_redshift_adapter.py
--- a/test/unit/test_redshift_adapter.py
+++ b/test/unit/test_redshift_adapter.py
@@ -190,7 +190,8 @@ def test_default_keepalive(self, psycopg2):
             password='password',
             port=5439,
             connect_timeout=10,
-            keepalives_idle=240
+            keepalives_idle=240,
+            application_name='dbt'
         )
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
@@ -207,7 +208,8 @@ def test_changed_keepalive(self, psycopg2):
             password='password',
             port=5439,
             connect_timeout=10,
-            keepalives_idle=256)
+            keepalives_idle=256,
+            application_name='dbt')
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
     def test_search_path(self, psycopg2):
@@ -224,7 +226,8 @@ def test_search_path(self, psycopg2):
             port=5439,
             connect_timeout=10,
             options="-c search_path=test",
-            keepalives_idle=240)
+            keepalives_idle=240,
+            application_name='dbt')
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
     def test_search_path_with_space(self, psycopg2):
@@ -241,7 +244,8 @@ def test_search_path_with_space(self, psycopg2):
             port=5439,
             connect_timeout=10,
             options=r"-c search_path=test\ test",
-            keepalives_idle=240)
+            keepalives_idle=240,
+            application_name='dbt')
 
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
     def test_set_zero_keepalive(self, psycopg2):
@@ -256,7 +260,8 @@ def test_set_zero_keepalive(self, psycopg2):
             host='thishostshouldnotexist',
             password='password',
             port=5439,
-            connect_timeout=10)
+            connect_timeout=10,
+            application_name='dbt')
 
     def test_dbname_verification_is_case_insensitive(self):
         # Override adapter settings from setUp()