【发布时间】:2021-10-28 18:46:47
【问题描述】:
在 cockroachdb 中,如果我从 SQL 终端运行 SHOW DATABASES;,我会得到:
database_name | owner | primary_region | regions | survival_goal
--------------------+-------+----------------+---------+----------------
_a66df261120b6c23 | root | NULL | {} | NULL
defaultdb | root | NULL | {} | NULL
postgres | root | NULL | {} | NULL
sammy | root | NULL | {} | NULL
system | node | NULL | {} | NULL
test123 | root | NULL | {} | NULL
test3 | root | NULL | {} | NULL
test4 | root | NULL | {} | NULL
test5 | root | NULL | {} | NULL
test9 | root | NULL | {} | NULL
(10 rows)
如果我现在运行SHOW TABLES FROM _a66df261120b6c23,我会得到:
schema_name | table_name | type | owner | estimated_row_count | locality
--------------+-------------------+-------+-------+---------------------+-----------
public | __Auth | table | root | 0 | NULL
public | tabDefaultValue | table | root | 0 | NULL
public | tabDocType Action | table | root | 0 | NULL
public | tabDocType Link | table | root | 0 | NULL
public | tabFile | table | root | 0 | NULL
public | tabSeries | table | root | 0 | NULL
public | tabSessions | table | root | 0 | NULL
public | tabSingles | table | root | 0 | NULL
public | tabdocfield | table | root | 0 | NULL
public | tabdocperm | table | root | 0 | NULL
public | tabdoctype | table | root | 0 | NULL
问题是当我尝试从 psycopg2 执行相同的查询时,即:SHOW TABLES FROM _a66df261120b6c23,我得到了错误:
test1 = cur.execute("show tables from _a66df261120b6c23;")
psycopg2.errors.InvalidCatalogName: database "root" does not exist
下面是python脚本:
with rdb_conn.cursor() as cur:
# cur.execute('CREATE database test9;')
test1 = cur.execute("show tables from _a66df261120b6c23;")
# print("test1: ", test1)
# result = cur.execute("CREATE DATABASE test9;")
rdb_conn.commit()
我应该提一下,创建数据库不是问题,例如,CREATE DATABASE test9 没有任何问题。所以我的问题是如何通过 cockorachdb 中的 psycopg2 获取给定数据库中的表?
cursor.execute("""SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public'""")
对我不起作用,_a66df261120b6c23 中的表格都没有显示
以下是用户列表:
username | options | member_of
--------------------+---------+------------
_a66df261120b6c23 | | {}
admin | | {}
doug | | {admin}
postgres | | {}
root | | {admin}
sammy | | {admin}
sammy2 | | {admin}
sammy3 | | {}
test_user | | {}
【问题讨论】:
标签: python psycopg2 cockroachdb