带有 timescaledb 扩展名的 postgresql 的 Sqlalchemy 设置

Yij*_*Tao 4 python postgresql sqlalchemy timescaledb

我试图将 sqlalchemy 与使用 timescaledb 扩展的底层 postgresql 连接起来。当我从 psql 终端客户端尝试它们时,所有查询都可以正常工作。但是当我尝试使用 python 和 sqlalchemy 来做这件事时,它一直给我一个错误。

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

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

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

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
Run Code Online (Sandbox Code Playgroud)

与db的连接很好,否则它不会知道db正在使用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()
Run Code Online (Sandbox Code Playgroud)

这是完全相同的错误消息:

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
Run Code Online (Sandbox Code Playgroud)

Gün*_*erl 5

这似乎与我的问题非常相似。

我猜你也更新到了新版本的 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()
Run Code Online (Sandbox Code Playgroud)

使用光标执行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;')
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

5297 次

最近记录:

5 年,8 月 前