Repository: dbt-labs/dbt-core

Issue:
Make connect_timeout for Postgres/Redshift Adapters Configurable
### Describe the feature
Make the connect_timeout for Postgres/Redshift Adapters configurable.

### Additional context
Affects both Redshift and Postgres adapters.

### Who will this benefit?
People who may be experience connection timeouts.

### Are you interested in contributing this feature?
Yes, will give it a try.



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
@@ -19,6 +19,7 @@ class PostgresCredentials(Credentials):
     user: str
     port: Port
     password: str  # on postgres the password is mandatory
+    connect_timeout: int = 10
     role: Optional[str] = None
     search_path: Optional[str] = None
     keepalives_idle: int = 0  # 0 means to use the default value
@@ -116,7 +117,7 @@ def open(cls, connection):
                 host=credentials.host,
                 password=credentials.password,
                 port=credentials.port,
-                connect_timeout=10,
+                connect_timeout=credentials.connect_timeout,
                 **kwargs)
 
             if credentials.role:


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
@@ -40,7 +40,7 @@ def setUp(self):
                     'host': 'thishostshouldnotexist',
                     'pass': 'password',
                     'port': 5432,
-                    'schema': 'public'
+                    'schema': 'public',
                 }
             },
             'target': 'test'
@@ -108,6 +108,37 @@ def test_cancel_open_connections_single(self):
 
         master.handle.get_backend_pid.assert_not_called()
 
+    @mock.patch('dbt.adapters.postgres.connections.psycopg2')
+    def test_default_connect_timeout(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_connect_timeout(self, psycopg2):
+        self.config.credentials = self.config.credentials.replace(connect_timeout=30)
+        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=30,
+            application_name='dbt')
+
     @mock.patch('dbt.adapters.postgres.connections.psycopg2')
     def test_default_keepalive(self, psycopg2):
         connection = self.adapter.acquire_connection('dummy')