暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL 访问 SQL Server 数据 - 外部表 3 - multicorn - sql_alchemy

digoal 2020-11-15
840

作者

digoal

日期

2020-11-15

标签

PostgreSQL , sql server , fdw , odbc , ogr , gdal , tds_fdw


背景

在PostgreSQL中通过外部表访问SQL Server的数据, 方法较多:

1、odbc_fdw

2、tds_fdw

3、ogr_fdw

4、multicorn sql_alchemy

Foreign Data Wrappers

Multicorn is bundled with a small set of Foreign Data Wrappers, which you can use or customize for your needs.

SQLAlchemy Foreign Data Wrapper

Class: multicorn.sqlalchemyfdw.SqlAlchemyFdw

Source code: multicorn/sqlalchemyfdw.py

Purpose

This fdw can be used to access data stored in a remote RDBMS. Through the use of sqlalchemy, many different rdbms engines are supported.

Dependencies

You will need the sqlalchemy library, as well as a suitable dbapi driver for the remote database.

You can find a list of supported RDBMs, and their associated dbapi drivers and connection strings in the sqlalchemy dialects documentation.

Required options

db_url (string)

An sqlalchemy connection string. Examples:

mysql: mysql://<user>:<password>@<host>/<dbname> mssql: mssql://<user>:<password>@<dsname>

See the sqlalchemy dialects documentation. for documentation.

tablename (string)

The table name in the remote RDBMS.

Allowed options

primary_key (string)

Identifies a column which is a primary key in the remote RDBMS. This options is required for INSERT, UPDATE and DELETE operations.

When defining the table, the local column names will be used to retrieve the remote column data. Moreover, the local column types will be used to interpret the results in the remote table. Sqlalchemy being aware of the differences between database implementations, it will convert each value from the remote database to python using the converter inferred from the column type, and convert it back to a postgresql suitable form.

What does it do to reduce the amount of fetched data?

  • quals are pushed to the remote database whenever possible. This include simple operators:
    • equality, inequality (=, <>, >, <, <=, >=)
    • like, ilike and their negations
    • IN clauses with scalars, = ANY (array)
    • NOT IN clauses, != ALL (array)
  • the set of needed columns is pushed to the remote_side, and only those columns will be fetched.

Usage example

For a connection to a remote mysql database (you’ll need a mysql dbapi driver, such as pymysql):

```
CREATE SERVER alchemy_srv foreign data wrapper multicorn options (
wrapper 'multicorn.sqlalchemyfdw.SqlAlchemyFdw'
);

create foreign table mysql_table (
column1 integer,
column2 varchar
) server alchemy_srv options (
tablename 'table',
db_url 'mysql://myuser:mypassword@myhost/mydb'
);
```

参考

https://github.com/CartoDB/odbc_fdw

http://pgxn.org/dist/tds_fdw/

http://www.postgresonline.com/journal/archives/355-Connecting-to-SQL-Server-from-Linux-using-FDWs.html

http://www.postgresonline.com/article_pfriendly/355.html

https://programmer.help/blogs/postgresql-connects-to-sql-server-using-odbc_fdw.html

https://wiki.postgresql.org/wiki/Foreign_data_wrappers

/etc/odbc.ini

/etc/odbcinst.ini

https://pypi.org/project/pyodbc/

https://docs.sqlalchemy.org/en/14/dialects/mssql.html#dsn-connections

http://www.sqlalchemy.org/

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论