dsn_port=8001# Specify your port number, e.g., 98939
dsn_uid="student"# Specify your username, e.g., "admin"
dsn_pwd="aidaho"# Specify your password, e.g., "xxx"
# Connect to the database
try:
connect=psycopg2.connect(
dbname=dsn_database,
user=dsn_uid,
password=dsn_pwd,
host=dsn_hostname,
port=dsn_port
)
print("Database Connected!")
# Check Connection
cursor=connect.cursor()
cursor.execute("SELECT version();")
record=cursor.fetchone()
print("You are connected to - ",record,"\n")
except (Exception,psycopg2.Error)aserror:
print("Error while connecting to PostgreSQL",error)
```
%% Output
Database Connected!
You are connected to - ('PostgreSQL 15.1 (Debian 15.1-1.pgdg110+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 10.2.1-6) 10.2.1 20210110, 64-bit',)