带有 timescaledb 扩展的 postgresql 的 SQLalchemy 设置

Sqlalchemy setup for postgresql with timescaledb extension

我试图将 sqlalchemy 与我的底层 postgresql 连接起来,后者使用 timescaledb 扩展。当我从 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。它基本上给出了相同的错误。 DB连接成功,但无法访问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

这似乎与 非常相似。

我猜你也更新了新版本的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;')