Skip to content

Commit 4ce19b0

Browse files
python: Use direct TLS connections for psycopg2 (#167)
Co-authored-by: Daniel Frankcom <frankcom@amazon.com>
1 parent 73a1a7f commit 4ce19b0

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

python/psycopg2/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ The code automatically detects the user type and adjusts its behavior accordingl
2929
* This code is not tested in every AWS Region. For more information, see
3030
[AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
3131

32+
## TLS connection configuration
33+
34+
This example uses direct TLS connections where supported, and verifies the server certificate is trusted. Verified SSL
35+
connections should be used where possible to ensure data security during transmission.
36+
37+
* Driver versions following the release of PostgreSQL 17 support direct TLS connections, bypassing the traditional
38+
PostgreSQL connection preamble
39+
* Direct TLS connections provide improved connection performance and enhanced security
40+
* Not all PostgreSQL drivers support direct TLS connections yet, or only in recent versions following PostgreSQL 17
41+
* Ensure your installed driver version supports direct TLS negotiation, or use a version that is at least as recent as
42+
the one used in this sample
43+
* If your driver doesn't support direct TLS connections, you may need to use the traditional preamble connection instead
44+
3245
## Run the example
3346

3447
### Prerequisites

python/psycopg2/src/example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import boto3
22
import psycopg2
3+
import psycopg2.extensions
34
import os
45
import sys
56

@@ -24,6 +25,10 @@ def create_connection(cluster_user, cluster_endpoint, region):
2425
"password": password_token
2526
}
2627

28+
# Use the more efficient connection method if it's supported.
29+
if psycopg2.extensions.libpq_version() >= 170000:
30+
conn_params["sslnegotiation"] = "direct"
31+
2732
# Make a connection to the cluster
2833
conn = psycopg2.connect(**conn_params)
2934

0 commit comments

Comments
 (0)