【问题标题】:How can I find a missed table in postgres如何在 postgres 中找到丢失的表
【发布时间】:2017-02-05 20:46:30
【问题描述】:

我正在使用 python psycopg2 库在 postgres 数据库中创建一个表:

self.conn=pg.connect(host='localhost',user='eba',password='****',database='eba')
cur=self.conn.cursor()
cur.execute(sql)
cur.close()

sql='''CREATE TABLE public.tempimport (
id integer NOT NULL DEFAULT nextval('tempimport_id_seq'::regclass),
tablename character varying(32) COLLATE pg_catalog."default",
    index_ character varying(32) COLLATE pg_catalog."default",
    CONSTRAINT tempimport_pkey PRIMARY KEY (id)
)
WITH (

OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.tempimport
    OWNER to eba;'''

cur=self.conn.cursor()
cur.execute(sql)
cur.close()

之后,如果我运行:

cur=self.conn.cursor()
cur.execute("SELECT count(*) FROM pg_catalog.pg_tables where tablename = 'tempimport'")
x=cur.fetchall()
print x

我得到1作为答案,即表存在。

但是,如果我使用 pgAdmin 登录相同的数据库/用户/密码并运行相同的 SELECT COUNT(*)... 语句,我会得到 0 作为答案。

我用代码创建的表在哪里?

我怎样才能找到它在哪里?

【问题讨论】:

  • 也许你也需要commit
  • 谢谢,就是这样!
  • 。 .得到我的次数比我想承认的要多。

标签: python postgresql


【解决方案1】:

尝试使用 information_schema:

SELECT * 
FROM information_schema.tables 
WHERE table_name = 'tempimport'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-22
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    相关资源
    最近更新 更多