安装 psycopg2 后尝试运行 python / django 时出现此错误:
错误:dlopen(/Users/macbook/Envs/medint/lib/python2.7/site-packages/psycopg2/_psycopg.so,2):找不到符号:_PQbackendPID 引用自:/Users/macbook/Envs/medint/lib /python2.7/site-packages/psycopg2/_psycopg.so 预期:动态查找
任何人?
我正在学习如何将 Postgres 与 Python (sqlalchemy) 结合使用。我安装时卡住了psycopg2。我有以下错误:
pg_config executable not found error
我知道这是一个非常常见的错误,并且已经有很多关于此问题的答案,但我找不到任何可以帮助我解决问题的内容。
我特别不明白这个答案:
通过附加以下内容将 Postgres 的路径添加到您的 .profile 文件中:
PATH="/Applications/Postgres.app/Contents/Versions/latest/bin:$PATH"
这是什么意思 ?我到底应该做什么?
我也尝试进入which -a pg_config我的终端,但没有任何反应。
多谢 !
我正在使用 python 2.7 和 postgresql 9.3 以及 psycopg2 2.7.3,当我尝试在参数传递时执行选择查询时,它会给我这个错误 psycopg2.ProgrammingError: 语法错误位于或接近“OR”
cur = con.cursor()
cur.execute('SELECT * FROM test WHERE voucher= ? OR voucher= ?', ('RHAT', 'MSO'))
Run Code Online (Sandbox Code Playgroud)
错误消息是
psycopg2.ProgrammingError:“OR”处或附近的语法错误
我有一个巨大的表(约 8 亿),我需要根据某些段条件获取数据。
数据:
d_id month_id sec average Class
89 201701 S 5.98 A
73 201703 N 7.63 B
31 201708 F 6.38 P
11 201709 K 6.38 P
Run Code Online (Sandbox Code Playgroud)
我有两个清单:
monthList = [201701,201702,201703]
Run Code Online (Sandbox Code Playgroud)
所以sql查询是:
sql_query = str("""select * from dbo.table_name where month_id IN monthList;""")
Run Code Online (Sandbox Code Playgroud)
现在我想将这些数据保存在服务器端游标中,并从中获取基于 classList 的子集
curs = cnxn.cursor('Class')
classList = ['A','B','P']
while True:
records = curs.fetchmany(int(1e3))
if not records:
break
for record in records:
# here I want to use the classList to subset the data , something …Run Code Online (Sandbox Code Playgroud) 我对使用 asyncio/aiohttp 很陌生,但我有一个 Python 脚本,它从 Postgres 表中读取一批 URL:s,下载 URL:s,在每次下载时运行处理函数(与问题无关) ,并将处理结果存回到表中。
简化形式如下所示:
import asyncio
import psycopg2
from aiohttp import ClientSession, TCPConnector
BATCH_SIZE = 100
def _get_pgconn():
return psycopg2.connect()
def db_conn(func):
def _db_conn(*args, **kwargs):
with _get_pgconn() as conn:
with conn.cursor() as cur:
return func(cur, *args, **kwargs)
conn.commit()
return _db_conn
async def run():
async with ClientSession(connector=TCPConnector(ssl=False, limit=100)) as session:
while True:
count = await run_batch(session)
if count == 0:
break
async def run_batch(session):
tasks = []
for url in get_batch():
task = asyncio.ensure_future(process_url(url, …Run Code Online (Sandbox Code Playgroud) 问题简要描述: Psycopg2 不会从 unittest 连接到 docker 内的测试数据库,但从控制台可以正常连接。
错误消息: psycopg2.OperationalError:服务器意外关闭了连接 这可能意味着服务器在处理请求之前或处理请求时异常终止。
详细信息: 我正在尝试在 docker 中设置一个测试数据库,该数据库将在测试之前创建并填充,然后在测试之后删除。
这是设置数据库的功能:
def set_up_empty_test_db():
client = docker.from_env()
try:
testdb = client.containers.get("TestDB")
testdb.stop()
testdb.remove()
testdb = client.containers.run(
"postgres",
ports={5432: 5433},
detach=True,
name="TestDB",
environment=["POSTGRES_PASSWORD=yourPassword"],
)
except NotFound:
testdb = client.containers.run(
"postgres",
ports={5432: 5433},
detach=True,
name="TestDB",
environment=["POSTGRES_PASSWORD=yourPassword"],
)
while testdb.status != "running":
testdb = client.containers.get("TestDB")
return
Run Code Online (Sandbox Code Playgroud)
如果我从控制台启动此函数,它将正常工作,不会出现错误,并且我可以看到 TestDB 容器正在运行。我可以成功启动连接:
conn = psycopg2.connect("host='localhost' user='postgres' password='yourPassword' port='5433'")
Run Code Online (Sandbox Code Playgroud)
但在单元测试时却不起作用。这是测试代码:
class TestCreateCity(unittest.TestCase):
def setUp(self):
set_up_empty_test_db()
con = psycopg2.connect("host='localhost' user='postgres' password='yourPassword' port='5433'")
self.assertIsNone(con.isolation_level)
cur = …Run Code Online (Sandbox Code Playgroud) 我在 PostgreSQL 中有三个物化视图,它们需要很长时间才能刷新(每个都超过几个小时),而且我需要每天刷新它们。
我目前正在使用一个 Python 脚本来执行此操作,该脚本一个接一个地刷新视图,但与在 pgAdmin 中手动刷新视图相比,它需要三倍的时间(我可以在不同的选项卡中同时运行所有三个刷新)。
这就是我的代码现在的样子:
import psycopg2
config = {'connection details'}
conn = psycopg2.connect(**config)
cur = conn.cursor()
# This is the part that I want to run simultaneously
cur.execute('REFRESH MATERIALIZED VIEW gsam.mv_hist_wip_data')
cur.execute('REFRESH MATERIALIZED VIEW gsam.mv_hist_ver_data')
cur.execute('REFRESH MATERIALIZED VIEW gsam.mv_hist_verda_data')
conn.close()
Run Code Online (Sandbox Code Playgroud)
如何REFRESH MATERIALIZED VIEW使用Python和psycopg2同时执行三个语句?
我正在尝试在 Mac OS Catalina 和 Python 3.8 上安装 psycopg2-binary。尝试使用 pip3 安装时,构建总是失败。Catalina 有什么解决方案吗?
“错误:psycopg2-binary 的构建轮失败”
我试图在Ubuntu 20.04安装Odoo 13,我已按照不同的程序来安装它像这一个的例子,但我总是得到这个psycopg2错误(下这一段),当我达到安装要求的组件的步骤.txt,这个错误只发生在python虚拟环境中。
错误:
ERROR: Command errored out with exit status 1:
command: /opt/odoo/odoo-venv/bin/python3 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-7jyb6cog/psycopg2/setup.py'"'"'; __file__='"'"'/tmp/pip-install-7jyb6cog/psycopg2/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-7c07yrdu
cwd: /tmp/pip-install-7jyb6cog/psycopg2/
Complete output (40 lines):
running bdist_wheel
running build
running build_py
creating build
creating build/lib.linux-x86_64-3.8
creating build/lib.linux-x86_64-3.8/psycopg2
copying lib/sql.py -> build/lib.linux-x86_64-3.8/psycopg2
copying lib/_ipaddress.py -> build/lib.linux-x86_64-3.8/psycopg2
copying lib/errorcodes.py -> build/lib.linux-x86_64-3.8/psycopg2
copying lib/_json.py -> build/lib.linux-x86_64-3.8/psycopg2
copying lib/tz.py -> build/lib.linux-x86_64-3.8/psycopg2
copying lib/__init__.py -> build/lib.linux-x86_64-3.8/psycopg2
copying lib/pool.py …Run Code Online (Sandbox Code Playgroud) 我在 Python 中将 postgres 与库一起使用psycopg2。连接到数据库后,我试图检查是否存在具有给定名称的表。在 postgres 中,我使用以下几行来执行此操作:
\connect myDB
select exists(select * from pg_tables where schemaname='public' AND tablename='mytable';)
Run Code Online (Sandbox Code Playgroud)
如果表存在,则此方法有效,但如果不存在,则此方法有效。在 python 中,我使用以下几行执行此操作:
import psycopg2 as pg
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT;
from psycopg2 import sql;
conn = pg.connect(user='postgres', host='localhost', password="pwd");
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT);
conn.autocommit = True
sql_table_check = sql.SQL("select exists(select * from pg_tables where schemaname='public' AND tablename={});")\
.format(sql.Identifier("mytable"));
cur = conn.cursor()
Run Code Online (Sandbox Code Playgroud)
但这是返回错误
psycopg2.errors.UndefinedColumn: column "mytable" does not exist
LINE 1: ...m pg_tables where schemaname='public' AND tablename="mytable");
Run Code Online (Sandbox Code Playgroud)
因为还没有创建这样的表。
检查 psycopg2 中是否存在列的正确方法是什么?
编辑
请注意,我想检查我连接的数据库中是否存在该表,我不介意它是否存在于另一个数据库中。
psycopg2 ×10
python ×8
postgresql ×6
django ×2
aiohttp ×1
bigdata ×1
cursor ×1
docker ×1
heroku ×1
macos ×1
odoo ×1
odoo-13 ×1
python-3.x ×1
ubuntu-20.04 ×1
unit-testing ×1
virtualenv ×1