-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_connection.py
More file actions
39 lines (33 loc) · 993 Bytes
/
Copy pathtest_connection.py
File metadata and controls
39 lines (33 loc) · 993 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import psycopg2
from psycopg2 import sql
# Replace these values with your actual credentials
host = "192.168.4.25"
port = 5432
database = "ecomm"
user = "postgres"
password = "<password>"
try:
# Connect to the PostgreSQL database
connection = psycopg2.connect(
host=host,
port=port,
database=database,
user=user,
password=password
)
print("Connection successful!")
# Create a cursor object to execute queries
cursor = connection.cursor()
# Execute a simple query to test the connection
cursor.execute("SELECT version();")
db_version = cursor.fetchone()
print("PostgreSQL database version:", db_version)
# Close the cursor and the connection
cursor.close()
connection.close()
print("Connection closed.")
except psycopg2.OperationalError as e:
print("OperationalError: Could not connect to the database.")
print("Details:", e)
except Exception as e:
print("An error occurred:", e)