Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 连接后,psycopg2找不到任何表_Python_Postgresql_Psycopg2 - Fatal编程技术网

Python 连接后,psycopg2找不到任何表

Python 连接后,psycopg2找不到任何表,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,我可以连接到我的数据库,但psycopg2找不到我的任何表。尝试获取我的用户时将出现以下错误: import psycopg2 try: conn = psycopg2.connect("dbname='pdb' user='postgres' host='localhost' password='password'") except: print 'failed to connect' cur = conn.cursor() cur.execute(""" SELECT *

我可以连接到我的数据库,但psycopg2找不到我的任何表。尝试获取我的用户时将出现以下错误:

import psycopg2

try:
    conn = psycopg2.connect("dbname='pdb' user='postgres' host='localhost' password='password'")
except:
    print 'failed to connect'

cur = conn.cursor()
cur.execute(""" SELECT * from Users """)
rows = cur.fetchall()
for row in rows:
    print row[0]

#Error:
psycopg2.ProgrammingError: relation "users" does not exist
LINE 1: SELECT * from Users 

# This also fails
cur.execute("""SELECT * from pdb.Users """)
如果我这样做:

cur.execute(""" SELECT * from pg_database """)

# Outputs
template1
template0
postgres
pdb
在我的管理面板中,pdb显示了一组表,其中一个是
用户
,所以我不确定为什么psycopg2找不到它

这里是来自psql的
pdb
的打印输出:

               List of relations
 Schema |        Name        | Type  |  Owner   
--------+--------------------+-------+----------
 public | Companies          | table | postgres
 public | Users              | table | postgres
(2 rows)

您的表名
用户
公司
都以大写字母开头。PostgreSQL将所有标识符转换为小写(默认情况下),如错误消息所示:

psycopg2.ProgrammingError: relation "users" does not exist
其中,
用户
用小写字母书写。如果您希望严格遵守SQL标准(PostgreSQL以其独特的特性而闻名),则需要这样做。您可以通过两种方式解决此问题:

在数据库中解决此问题:

遵循通用约定,并将表重命名为全小写

在代码中解决它:

引用您的标识符(本例中为表名),以便PostgreSQL保持其不变:

cur.execute(""" SELECT * from "Users" """)

当您连接到错误的数据库时,通常也会发生此问题。就我而言,我连接到了错误的数据库 下面是我使用psycopg2库连接的代码

import psycopg2
try:
            src_conn = psycopg2.connect("dbname={} user={} host=localhost password={}".format(self.src_db, self.src_db, self.src_url ,self.src_pwd))
            if src_conn:
                src_curr = src_conn.cursor()
                print("Cursor found",src_curr)
                src_curr.execute("select * from product_template")
                recs = src_curr.fetchall()
                print(recs)
            else:
                print("Cursor not found")
        except Exception as ex:
            print(ex)

你救了我一天。我不知道那个“标准”,老实说,这很奇怪。谢谢