【问题标题】:Sqlalchemy setup for postgresql with timescaledb extension [duplicate]带有timescaledb扩展的postgresql的Sqlalchemy设置[重复]
【发布时间】:2018-09-10 18:01:59
【问题描述】:

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

这是我尝试使用的非常基本的代码 sn-p:

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

与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()

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

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

【问题讨论】:

标签: python postgresql sqlalchemy timescaledb


【解决方案1】:

这似乎与my issue 非常相似。

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

【讨论】:

  • “我的问题”链接已损坏。
猜你喜欢
  • 2021-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多