使用TimescaleDB扩展配置PostgreSQL的Sqlalchemy设置

8

我想将sqlalchemy与使用timescaledb扩展的底层postgresql连接起来。当我尝试从psql终端客户端运行所有查询时,它们都可以正常工作。但是,当我尝试使用Python和sqlalchemy时,它一直抛出错误。

这是我尝试测试的非常基本的代码片段:

engine = create_engine('postgres://usr:pwd@localhost:5432/postgres', echo=True)
engine.execute('select 1;')

它总是显示以下错误信息:

File "/home/usr/.local/share/virtualenvs/redbird-lRSbFM0t/lib/python3.6/site-packages/psycopg2/extras.py", line 917, in get_oids
""" % typarray)
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) could not access file "timescaledb-0.9.0": No such file or directory

数据库连接正常,否则它将不知道数据库正在使用timescaledb。

有人有什么见解吗?

更新:我尝试直接使用psycopg2。它基本上给出了相同的错误。数据库已成功连接,但无法访问timescaledb-0.9.0。

这是代码片段:

conn_string = "host='localhost' dbname='db' user='usr' password='pwd'"
print("Connecting to database\n ->%s " % (conn_string))

conn = psycopg2.connect(conn_string)
cursor = conn.cursor()
print("Connected!\n")

cursor.execute("\dx")
records = cursor.fetchall()

以下是完全相同的错误信息:

Connecting to database
Connected!

Traceback (most recent call last):
File "/home/usr/Workspace/somepath/web/model/model.py", line 21, in <module>
cursor.execute("\dx")
psycopg2.OperationalError: could not access file "timescaledb-0.9.0": No such file or directory

可能需要安装最新版本的 Psycopg: http://initd.org/psycopg/docs/install.html - Rehan Azher
我正在使用我认为是最新版本的psycopg2 2.7.4来运行它。@RehanAzher - Yijie Tao
1个回答

7
这似乎与我的问题非常相似。
我猜你也更新了一个新版本的Timescale?事实是:每次更新timescale包后,你不仅需要确保库已预加载(如命令行上的警告所示),还必须通过psql手动升级使用扩展的每个数据库
请参阅我的回答以获取步骤。
--
这个片段对我有用:
#! /usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2

# Connect to an existing database.
conn = psycopg2.connect(dbname='my-db-name',
                        user='postgres',
                        password='super-secret',
                        host='localhost',
                        port='5432')

# Open a cursor to perform database operations.
cur = conn.cursor()

# Query the database and obtain data as Python objects.
cur.execute('SELECT * FROM my-table-name LIMIT 100 ;')

# Print results.
results = cur.fetchall()
for result in results:
    print(result)

# Close communication with the database.
cur.close()
conn.close()

使用光标执行psql命令对我来说也无法正常工作。我认为这并不是应该的。但是可靠的方法是执行SQL语句:

# Check if the database has the timescaledb extension installed.
# This is about the same as xecuting '\dx' on psql.
cur.execute('SELECT * from pg_extension;')

“我的问题”链接已损坏。 - Jérôme

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接