You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
auto/test/test_connent_pgsql.py

40 lines
1.0 KiB

# -*- coding: utf-8 -*-
import psycopg2
# PostgreSQL数据库连接参数
host = "192.168.100.122:20788"
dbname = "postgres"
user = "freshrss"
password = "freshrss"
# 连接到PostgreSQL数据库
conn = psycopg2.connect(host=host, dbname=dbname, user=user, password=password)
conn.autocommit = True
# 创建一个cursor对象
cur = conn.cursor()
# 获取所有数据库的名称
cur.execute("SELECT datname FROM pg_database WHERE datistemplate = false;")
databases = cur.fetchall()
print("数据库列表:")
for db in databases:
print(db[0])
# 连接到具体的数据库
conn.close()
conn = psycopg2.connect(dbname=db[0], user=user, password=password, host=host)
cur = conn.cursor()
# 获取该数据库中的所有表
cur.execute("SELECT table_name FROM information_schema.tables WHERE table_schema='public';")
tables = cur.fetchall()
print(f"\n数据库 '{db[0]}' 的表列表:")
for table in tables:
print(table[0])
# 关闭cursor和连接
cur.close()
conn.close()