ZhangZhihui's Blog  

GaussDB (openGauss / Huawei GaussDB) is PostgreSQL-compatible, so you can use the standard psycopg2 driver (or SQLAlchemy on top of it) to connect.

Here’s a minimal working example in Python using psycopg2:

import psycopg2

# Replace with your GaussDB connection details
conn = psycopg2.connect(
    host="your-gaussdb-host",   # e.g. 192.168.1.100
    port="8000",                # GaussDB default port (sometimes 5432 or 8000)
    database="your_database",
    user="your_username",
    password="your_password",
    sslmode="require"           # optional, depending on server config
)

# Create a cursor
cur = conn.cursor()

# Run a simple query
cur.execute("SELECT version();")

# Fetch result
version = cur.fetchone()
print("GaussDB version:", version)

# Cleanup
cur.close()
conn.close()

 


If you want SQLAlchemy (higher-level):

from sqlalchemy import create_engine

# Format: postgresql+psycopg2://user:password@host:port/database
engine = create_engine("postgresql+psycopg2://your_username:your_password@your-gaussdb-host:8000/your_database")

with engine.connect() as conn:
    result = conn.execute("SELECT NOW();")
    for row in result:
        print("Current time:", row[0])

 


🔹 Notes:

  • GaussDB uses a PostgreSQL wire protocol, so PostgreSQL drivers work.

  • If you’re on Huawei Cloud GaussDB (for openGauss), sometimes the port is 8000 (not 5432).

  • If SSL is enforced, you may need sslmode=require or to provide certificates.

 

posted on 2025-09-22 16:36  ZhangZhihuiAAA  阅读(15)  评论(0)    收藏  举报