为什么我在pgAdmin中看不到我在python中创建的表

问题描述 投票:0回答:1

我在 jupyter 笔记本中使用 psycopg2 创建了一个表。 这已成功完成,并且我没有收到任何错误。现在我想在 pgAdmin 界面中查看该表,但它没有出现在那里。为什么?

这是我写的代码:

import psycopg2
from psycopg2 import Error
try:
    connection = psycopg2.connect(user = "postgres",
                                  password = "nadav256",
                                  port = "5432",
                                  database = "postgres")
    cursor = connection.cursor()

    create_table_query = '''CREATE TABLE demo2
          (ID INT PRIMARY KEY,
          TIME           VARCHAR(50),
          ACCABS         REAL,
          BARO           REAL); '''

    cursor.execute(create_table_query)
    connection.commit()
    print("Table created successfully in PostgreSQL ")
except (Exception, psycopg2.DatabaseError) as error :
    print ("Error while creating PostgreSQL table", error)
finally:
    #closing database connection.
        if(connection):
            cursor.close()
            connection.close()
            print("PostgreSQL connection is closed")

这是我得到的输出:

Table created successfully in PostgreSQL 
PostgreSQL connection is closed
python sql database postgresql
1个回答
0
投票

也许,在

host
中显式指定
psycopg2.connect()
参数。在 psycopg2 文档中,如果没有指定,默认情况下它将是 UNIX 套接字。

并确保用户的默认模式;通常默认情况下,架构是

public
,因此当我们运行通用
CREATE TABLE demo2...
时,它实际上会创建一个表
public.demo2

© www.soinside.com 2019 - 2024. All rights reserved.