Created
May 22, 2025 13:22
-
-
Save thibaultserti/6c7ba8077c3394f25161fbace4bd77e0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def connect_to_postgres(host, port, dbname, user, password): | |
try: | |
conn = psycopg2.connect( | |
host=host, | |
port=port, | |
dbname=dbname, | |
user=user, | |
password=password | |
) | |
print("✅ Connexion réussie à PostgreSQL") | |
return conn | |
except OperationalError as e: | |
print("❌ Erreur de connexion :", e) | |
return None | |
conn = connect_to_postgres( | |
host="localhost", | |
port=5432, | |
dbname="ma_base", | |
user="mon_user", | |
password="mon_mot_de_passe" | |
) | |
if conn: | |
# Créer un curseur | |
cur = conn.cursor() | |
cur.execute("SELECT version();") | |
version = cur.fetchone() | |
print("Version PostgreSQL :", version) | |
# Fermer le curseur et la connexion | |
cur.close() | |
conn.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment