这个技能允许你连接 PostgreSQL 数据库并执行各种查询操作。
配置文件 db_config.json 位于技能目录的 config/ 子目录下。脚本会自动搜索并加载配置文件,无需用户手动指定。
配置文件搜索顺序:
db_config.json
config/db_config.json(默认位置)
db_config.json
如果配置文件不存在,脚本会提示用户创建。
Windows - 一键安装:
# 方式 1:使用批处理脚本(推荐)
cd .qoder/skills/postgres-tool
.\scripts\install-offline.bat
# 方式 2:使用 PowerShell 脚本
cd .qoder/skills/postgres-tool
.\scripts\install-offline.ps1
Linux/Mac - 手动安装:
cd .qoder/skills/postgres-tool
python -m pip install --no-index --find-links=./scripts/dependencies -r scripts/requirements.txt
重要提示:
python -m pip 确保即使 pip 不在 PATH 中也能工作
如果机器可以访问互联网:
pip install psycopg2-binary pandas openpyxl numpy python-dateutil tzdata et-xmlfile
如果安装失败,首先运行诊断工具检查问题:
# 进入技能目录
cd .qoder/skills/postgres-tool
# 运行诊断脚本
python scripts/diagnose_deps.py
诊断工具会自动检查:
根据诊断结果采取相应措施。
错误示例:
ERROR: Could not find a version that satisfies the requirement psycopg2-binary
(from versions: none)
ERROR: No matching distribution found psycopg2-binary
原因分析:
dependencies 目录中的 wheel 文件与当前 Python 版本不匹配
cp313 表示 Python 3.13)
解决方案:
步骤 1:检查 Python 版本
python --version
步骤 2:检查 wheel 文件是否匹配
查看 dependencies 目录下的文件名,例如:
psycopg2_binary-2.9.11-cp313-cp313-win_amd64.whl → 需要 Python 3.13
pandas-3.0.1-cp313-cp313-win_amd64.whl → 需要 Python 3.13
如果文件名中的 cp313 与你的 Python 版本不一致,需要重新下载对应版本的 wheel 文件。
步骤 3:使用正确的命令重新下载依赖
# 进入技能目录
cd .qoder/skills/postgres-tool
# 删除旧的依赖文件(如果有)
rm -rf scripts/dependencies/*.whl # Linux/Mac
del /Q scripts\dependencies\*.whl # Windows
# 下载与当前 Python 版本匹配的 wheel 文件
pip download psycopg2-binary pandas openpyxl numpy python-dateutil tzdata et-xmlfile -d scripts/dependencies
步骤 4:重新运行安装脚本
# Windows
.\install-dependencies.bat
# Linux/Mac
./install-dependencies.sh
解决方案:
在有网络的机器上先下载依赖包:
# 1. 在外网机器下载
pip download psycopg2-binary pandas openpyxl numpy python-dateutil tzdata et-xmlfile -d ./offline-deps
# 2. 将整个文件夹复制到内网机器的技能目录
cp -r offline-deps /path/to/postgres-tool/scripts/dependencies
# 3. 在内网运行安装脚本
cd /path/to/postgres-tool
./scripts/install-dependencies.sh
症状:
ERROR: Corrupted or incomplete wheel file
解决方案:
# 删除所有 wheel 文件并重新下载
cd .qoder/skills/postgres-tool
rm -rf scripts/dependencies/*.whl # Linux/Mac
del /Q scripts\dependencies\*.whl # Windows
# 重新下载
pip download psycopg2-binary pandas openpyxl numpy python-dateutil tzdata et-xmlfile -d scripts/dependencies
# 验证文件大小(应该都大于 10KB)
ls -lh scripts/dependencies/*.whl
症状:
PermissionError: [Errno 13] Permission denied
解决方案:
# Windows:以管理员身份运行 PowerShell
# Linux/Mac:使用 sudo
sudo ./install-dependencies.sh
# 或者安装到用户目录
pip install --user --no-index --find-links=./scripts/dependencies -r scripts/requirements.txt
当用户请求查询数据库时,直接调用 scripts/postgres_tool.py 脚本执行查询,不要自己写脚本。
执行查询:
python scripts/postgres_tool.py "SELECT * FROM table_name LIMIT 10;"
列出所有表:
python scripts/postgres_tool.py --list-tables
查看表结构:
python scripts/postgres_tool.py --schema table_name
重要安全机制:
# 更新用户状态(会自动备份并请求确认)
python scripts/postgres_tool.py --update "UPDATE users SET status='active' WHERE last_login > '2024-01-01';" --table users
# 强制执行(跳过确认,危险!)
python scripts/postgres_tool.py --update "UPDATE products SET price=price*0.9;" --table products --force
# 删除过期数据(会自动备份并请求确认)
python scripts/postgres_tool.py --delete "DELETE FROM logs WHERE created_at < '2023-01-01';" --table logs
# 预览模式(只显示不执行)
python scripts/postgres_tool.py --delete "DELETE FROM temp_data;" --table temp_data --dry-run
如果执行了 UPDATE 或 DELETE 操作,可以通过备份文件恢复数据:
# 从备份恢复数据
python scripts/postgres_tool.py --restore backups/20260322_184500/users_all_20260322_184500.csv
# 预览恢复内容(不实际执行)
python scripts/postgres_tool.py --restore backups/20260322_184500/users_all_20260322_184500.csv --dry-run
备份文件包含:
提供以下脚本来查看数据库表信息:
def list_tables():
"""列出所有表"""
sql = """
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
"""
return execute_query(sql)
def get_table_schema(table_name):
"""获取表结构信息"""
sql = """
SELECT
column_name,
data_type,
is_nullable,
column_default
FROM information_schema.columns
WHERE table_name = %s
ORDER BY ordinal_position;
"""
return execute_query(sql, params=(table_name,))
def get_table_indexes(table_name):
"""获取表的索引信息"""
sql = """
SELECT
indexname,
indexdef
FROM pg_indexes
WHERE tablename = %s;
"""
return execute_query(sql, params=(table_name,))
支持导出为 CSV 或 Excel 格式:
def export_to_csv(df, filename):
"""将 DataFrame 导出为 CSV 文件"""
df.to_csv(filename, index=False, encoding='utf-8-sig')
print(f"已导出到 {filename}")
def export_to_excel(df, filename, sheet_name='Sheet1'):
"""将 DataFrame 导出为 Excel 文件"""
df.to_excel(filename, index=False, sheet_name=sheet_name, engine='openpyxl')
print(f"已导出到 {filename}")
# 使用示例
df = execute_query("SELECT * FROM your_table;")
export_to_csv(df, "query_result.csv")
export_to_excel(df, "query_result.xlsx")
在查询时要注意的错误情况:
import psycopg2.errors
def safe_execute_query(sql, max_rows=1000):
"""安全地执行查询,限制返回行数"""
try:
# 自动添加 LIMIT 防止返回过多数据
if 'LIMIT' not in sql.upper():
sql = f"{sql.rstrip(';')} LIMIT {max_rows}"
with get_db_connection() as conn:
df = pd.read_sql_query(sql, conn)
return df
except psycopg2.errors.SyntaxError as e:
print(f"SQL 语法错误:{e}")
raise
except psycopg2.errors.UndefinedTable as e:
print(f"表不存在:{e}")
raise
except Exception as e:
print(f"查询失败:{e}")
raise
当用户说:"帮我查一下 users 表的前 100 条记录,并导出为 Excel"
步骤:
db_config.json
SELECT * FROM users LIMIT 100;
users_export.xlsx
# 列出所有表
list_tables()
# 查看表结构
get_table_schema('table_name')
# 执行自定义查询
execute_query("SELECT column1, column2 FROM table WHERE condition;")
# 导出结果
df = execute_query("SELECT * FROM table;")
export_to_csv(df, "output.csv")
export_to_excel(df, "output.xlsx")
问题:无法连接数据库
db_config.json 中的配置是否正确
问题:查询超时
问题:中文乱码
encoding='utf-8-sig'
共 1 个版本
暂无安全检测报告