将 postgresql 与 sqlalchemy 连接
我知道这可能确实是一个简单的问题,但我不知道解决方案。当我尝试连接到 postgresql 时发生了什么?我是数据库和编程领域的自学者,所以请温柔地对待我。 当我尝试以下代码时:
import sqlalchemy
db = sqlalchemy.create_engine('postgresql:///tutorial.db')
我收到此错误:
回溯(最近一次调用最后一次): 文件“”,第 1 行,位于 db = sqlalchemy.create_engine('postgresql:///tutorial.db') 文件“C:\Python27\lib\site-packages\sqlalchemy-0.7.5dev-py2.7.egg\sqlalchemy\engine__init__.py”,第 327 行,在 create_engine 中 返回策略.create(*args, **kwargs) 文件“C:\Python27\lib\site-packages\sqlalchemy-0.7.5dev-py2.7.egg\sqlalchemy\engine\strategies.py”,第 64 行,在创建中 dbapi = dialect_cls.dbapi(**dbapi_args) 文件“C:\Python27\lib\site-packages\sqlalchemy-0.7.5dev-py2.7.egg\sqlalchemy\dialects\postgresql\psycopg2.py”,第 289 行,在 dbapi 中 psycopg = 导入('psycopg2') 导入错误:没有名为 psycopg2 的模块
我需要单独安装 psycopg2 吗? postgresql 的正确连接字符串是什么?
I know this might be really a simple question but I don't know the solution. What is happening here when I try to connect to postgresql? I am self learner in this field of database and programming so please be gentle with me.
When I try following code:
import sqlalchemy
db = sqlalchemy.create_engine('postgresql:///tutorial.db')
I get this error:
Traceback (most recent call last):
File "", line 1, in
db = sqlalchemy.create_engine('postgresql:///tutorial.db')
File "C:\Python27\lib\site-packages\sqlalchemy-0.7.5dev-py2.7.egg\sqlalchemy\engine__init__.py", line 327, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Python27\lib\site-packages\sqlalchemy-0.7.5dev-py2.7.egg\sqlalchemy\engine\strategies.py", line 64, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "C:\Python27\lib\site-packages\sqlalchemy-0.7.5dev-py2.7.egg\sqlalchemy\dialects\postgresql\psycopg2.py", line 289, in dbapi
psycopg = import('psycopg2')
ImportError: No module named psycopg2
Do I need to install psycopg2 separately? What is the correct connection string for postgresql?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要
pip install SQLAlchemy
和pip install psycopg2
。使用 psycopg2 的 SQLAlchemy 连接字符串的示例:
您还可以仅使用 psycopg2 驱动程序连接到数据库:
但是,使用 psycopg2 驱动程序进行连接不会利用 SQLAlchemy。
You would need to
pip install SQLAlchemy
andpip install psycopg2
.An example of a SQLAlchemy connection string that uses psycopg2:
You could also connect to your database using the psycopg2 driver exclusively:
However, using the psycopg2 driver to connect does not take advantage of SQLAlchemy.
是的,psycopg2 基本上是 PostgreSQL 的 Python 驱动程序,需要单独安装。
可以在此处找到有效连接字符串的列表,您的连接字符串有点偏离(您需要以下链接中指定的用户名、密码和主机名):
http://docs.sqlalchemy.org/en/latest/core/engines.html#postgresql
Yes, psycopg2 are basically the Python drivers for PostgreSQL that need to be installed separately.
A list of valid connection strings can be found here, yours is a bit off (you need to the username, the password and hostname as specified in the link below):
http://docs.sqlalchemy.org/en/latest/core/engines.html#postgresql
是的,您需要单独安装 psycopg2,如果您使用的是 linux,您只需在终端中输入以下行即可:
$pip install psycopg2
如果这不起作用,请尝试使用 sudo:$sudo pip install psycopg2
Yes, you need to install psycopg2 separately, if you're using linux you can simply enter the following line to the terminal:
$pip install psycopg2
if this doesn't work try using sudo:$sudo pip install psycopg2