使用 python 的 odo 插入特定于模式的 table

Inserting to schema-specific table with python's odo

我正在使用 python 的 odo 将数据从 pandas 数据帧移动到 postgresql 数据库。目标是每个 "user" 在他们的模式中看到他们自己的数据,但在 "users" 之间具有相同的数据模型和 table/view 命名模式。使用正常 SQL 我可以这样做:

CREATE SCHEMA my_schema;
CREATE TABLE my_schema.my_table AS select 1;

我的数据库 URI 如下所示

db_uri = 'postgresql://localhost/postgres::my_schema.my_table'

这在名为 "my_schema.my_table" 的 default 模式中为我提供了 tables,包括 '.'在 table 名称中,而不是在架构 "my_schema".

中名为 "my_table" 的 table

我根据这个github issue尝试了不同的组合,例如:

db_uri = 'postgresql://localhost/postgres.schema::tmp')

这给了我这个 Traceback

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL:  database "postgres/schema" does not exist

还有这个

db_uri = 'postgresql://localhost/postgres::my_schema/my_table'

这给了我 table 个名为 "my_schema/my_table".

这是一个示例代码:

import pandas as pd
from odo import odo
db_uri = 'postgresql://localhost/postgres::my_schema.my_table'
odo(pd.DataFrame([{'a': 1}, {'a': 1}]), db_uri)

隐藏在 mailing list for blaze 的深处是对 schema 参数的提及

d = Data(resource('postgresql://localhost/db::t', schema='myschema'))

可与 odo 一起使用,格式如下:

from odo import odo, drop
drop(db_uri, schema='my_schema') # to drop table in specific schema
odo(data, db_uri, schema='my_schema')

工作代码

import pandas as pd
from odo import odo
db_uri = 'postgresql://localhost/postgres::my_table'
odo(pd.DataFrame([{'a': 1}, {'a': 1}]), db_uri, schema='my_schema')