You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中psycopg2无法找到PostgreSQL中已存在的employees表

psycopg2无法识别PostgreSQL中已存在的employees表

环境

  • WSL Ubuntu 20.04发行版
  • PostgreSQL 12.18
  • Python psycopg2库

问题现象

执行以下Python代码读取example_db中的employees表时:

import psycopg2

conn = psycopg2.connect(
    host="localhost",
    database="example_db",
    user="my_user",
    password="my_pw"
)
cur = conn.cursor()
cur.execute("SELECT * FROM employees")
rows = cur.fetchall()
cur.close()

触发错误:

psycopg2.errors.UndefinedTable: relation "employees" does not exist
LINE 1: SELECT * FROM employees

但通过psql命令行直接连接查询时,能正常获取数据:

$ psql -U my_user -d example_db
psql (12.18 (Ubuntu 12.18-0ubuntu0.20.04.1))
Type "help" for help.

example_db=# SELECT * FROM employees;
 id |     name     | age | position
----+--------------+-----+-----------
  1 | John Doe     |  30 | Manager
  2 | Jane Smith   |  25 | Developer
  3 | Mike Johnson |  35 | Designer
(3 rows)

补充信息

数据库列表:

$ sudo -u postgres psql
psql (12.18 (Ubuntu 12.18-0ubuntu0.20.04.1))

postgres=# \l
                              List of databases
    Name    |  Owner   | Encoding | Collate |  Ctype  |   Access privileges
------------+----------+----------+---------+---------+-----------------------
 example_db | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =Tc/postgres         +
            |          |          |         |         | postgres=CTc/postgres+
            |          |          |         |         | my_user=CTc/postgres
 postgres   | postgres | UTF8     | C.UTF-8 | C.UTF-8 |
 template0  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
            |          |          |         |         | postgres=CTc/postgres
 template1  | postgres | UTF8     | C.UTF-8 | C.UTF-8 | =c/postgres          +
            |          |          |         |         | postgres=CTc/postgres
(4 rows)

解决方法

1. 检查表所属的schema

PostgreSQL默认使用public schema,但如果表不在该schema下,或连接的search_path未包含目标schema,就会找不到表:

  • 先在psql中查询表的完整位置:
    example_db=# SELECT schemaname, tablename FROM pg_tables WHERE tablename = 'employees';
    
  • 如果表不在public下,Python代码中需指定schema:
    cur.execute("SELECT * FROM 目标schema名.employees")
    
  • 或在连接时设置search_path,确保包含目标schema:
    conn = psycopg2.connect(
        host="localhost",
        database="example_db",
        user="my_user",
        password="my_pw",
        options="-c search_path=目标schema名,public"
    )
    

2. 排查表名大小写问题

如果创建表时使用了双引号(比如"Employees"),PostgreSQL会严格区分大小写,此时查询必须带双引号:

cur.execute('SELECT * FROM "Employees"')

3. 验证连接的数据库是否正确

可以在代码中先确认当前连接的数据库,避免连接到错误库:

cur.execute("SELECT current_database();")
print(cur.fetchone())

确保输出为example_db。

4. 确认用户权限

虽然命令行能访问,但需确保my_user对employees表有SELECT权限:

example_db=# SELECT table_name, privilege_type FROM information_schema.table_privileges WHERE grantee = 'my_user' AND table_name = 'employees';

如果缺少权限,执行授权:

example_db=# GRANT SELECT ON employees TO my_user;

内容的提问来源于stack exchange,提问作者Danj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 15:53:11