Python PostgreSQL 模块。哪个最好?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/144448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:35:12  来源:igfitidea点击:

Python PostgreSQL modules. Which is best?

pythonpostgresqlmodule

提问by jmissao

I've seen a number of postgresql modules for python like pygresql, pypgsql, psyco. Most of them are Python DB API 2.0 compliant, some are not being actively developed anymore. Which module do you recommend? Why?

我见过许多用于 python 的 postgresql 模块,如 pygresql、pypgsql、psyco。它们中的大多数都符合 Python DB API 2.0,有些不再被积极开发。你推荐哪个模块?为什么?

采纳答案by Jim

psycopg2 seems to be the most popular. I've never had any trouble with it. There's actually a pure Python interface for PostgreSQL too, called bpgsql. I wouldn't recommend it over psycopg2, but it's recently become capable enough to support Django and is useful if you can't compile C modules.

psycopg2 似乎是最受欢迎的。我从来没有遇到过任何麻烦。实际上还有一个用于 PostgreSQL 的纯 Python 接口,称为bpgsql。我不会在 psycopg2 上推荐它,但它最近变得足以支持 Django,如果你不能编译 C 模块,它会很有用。

回答by jdcaron

I suggest Psycopg over Psycopg2 since the first one seems a bit more sable. At least in my experience. I have an application running 24/7 and sometimes I was getting random memory crashes (double free or corruption errors) from Psycopg2. Nothing I could have debug fast or easily since it's not a Python error but a C error. I just switched over to Pyscopg and I did not get any crashes after that.

我建议 Psycopg 而不是 Psycopg2,因为第一个看起来更黑。至少在我的经验中。我有一个 24/7 全天候运行的应用程序,有时我会从 Psycopg2 获得随机内存崩溃(双重释放或损坏错误)。我无法快速或轻松地进行调试,因为它不是 Python 错误而是 C 错误。我刚刚切换到 Pyscopg,之后我没有遇到任何崩溃。

Also, as said in an other post, bpgsqlseems a very good alternative. It's stable and easy to use since you don't need to compile it. The only bad side is that library is not threadsafe.

此外,正如在另一篇文章中所说,bpgsql似乎是一个非常好的选择。它稳定且易于使用,因为您不需要编译它。唯一不好的一面是该库不是线程安全的。

Pygresql seems nice, there's a more direct way to query the database with this library. But I don't know how stable this one is though.

Pygresql 看起来不错,有一种更直接的方法可以使用此库查询数据库。但是我不知道这个有多稳定。

回答by Hugo Mota

In my experience, psycopg2 is the most used library for this. Like you said, it's DB API 2.0 compliant, wich gives a solid interface to work with.

根据我的经验,psycopg2 是最常用的库。就像你说的,它符合 DB API 2.0,提供了一个可靠的界面来使用。

For those who find the standard API to be a little too verbose and hard to work with, I made a small library that might help:

对于那些发现标准 API 有点过于冗长且难以使用的人,我创建了一个可能有帮助的小库:

https://github.com/hugollm/rebel

https://github.com/hugollm/rebel

回答by Szymon Lipiński

I uses only psycopg2 and had no problems with that.

我只使用 psycopg2 并且没有问题。

回答by Chmod

I've used pg8000 without any problems in the past 3 years. It's uptodate and available on pypi and works on both python2 and python3. You can use "pip install pg8000" to quickly get it (don't forget to use --proxy=yourproxy:yourport if you're behind a firewall).

在过去的 3 年里,我使用 pg8000 没有任何问题。它是最新的,可在 pypi 上使用,并且适用于 python2 和 python3。您可以使用“pip install pg8000”来快速获取它(如果您在防火墙后面,请不要忘记使用 --proxy=yourproxy:yourport )。

If you're worried about thread safety, it also provides a score for thread safety (see: http://pybrary.net/pg8000/dbapi.htmland https://www.python.org/dev/peps/pep-0249/for definitions of the different levels of thread safety) (although I have no yet used threads with psql).

如果您担心线程安全,它还提供了线程安全的分数(请参阅:http: //pybrary.net/pg8000/dbapi.htmlhttps://www.python.org/dev/peps/pep- 0249/用于定义不同级别的线程安全)(尽管我还没有将线程与 psql 一起使用)。

回答by zgoda

Psycopg1 is known for better performance in heavilyy threaded environments (like web applications) than Psycopg2, although not maintained. Both are well written and rock solid, I'd choose one of these two depending on use case.

尽管没有维护,但 Psycopg1 在重线程环境(如 Web 应用程序)中的性能比 Psycopg2 更好。两者都写得很好,坚如磐石,我会根据用例选择这两者之一。