#首先安装psycopg2模块 import psycopg2 # 配置数据库连接参数 conn_params = { "dbname": "your_db", "user": "your_user", "password": "your_password", "host": "your_host", "port": "5432" } # 尝试连接数据库 try: conn = psycopg2.connect(**conn_params) print("连接成功") # 创建一个游标对象 cur = conn.cursor() # 编写SQL查询 cur.execute("SELECT version();") # 获取查询结果 db_version = cur.fetchone() print(db_version) # 关闭游标 cur.close() # 关闭连接 conn.close() except psycopg2.Error as e: print("数据库连接失败:", e)
标签:cur,登录,python,psycopg2,version,print,your,conn,postgres From: https://www.cnblogs.com/ada-tina/p/18197420