您如何获得在pgadmin中创建的Postgresql数据库的URL?

问题描述 投票:-1回答:2

所以我创建了一个postgresql数据库,我想使用sqlalchemy将其链接到我的python代码,但是由于我是使用pgadmin创建数据库的,所以我不知道在代码中提到数据库的URL是什么。预先谢谢你。

python postgresql sqlalchemy pgadmin
2个回答
1
投票

如果使用默认值创建数据库,则可能可以使用psql(SQL Shell)登录到服务器。

尝试连接到您创建的数据库,如果能够连接到数据库,请尝试\conninfo

如果是在您自己的计算机上,则输出将是非常标准的主机,将是localhost,ip将是127.0.0.1,端口将是默认端口5432

Screenshot of a psql shell with an example of the command conninfo

一旦确定了这些内容,就应该尝试使用此answer to a different question中的以下代码连接到数据库。

在尝试连接之前,请确保同时安装了SQLAlchemy和psycopg2。然后试试这个:

from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://user:password@hostname/database_name')

或者可能会找到有关SQLAlchemy的很好的教程。


0
投票

返回pgAdmin,单击服务器的节点,然后在“属性”页面的“连接”下检查值:

pgAdmin

根据the SQLAlchemy documentation,这些值的对应连接URL为

connection_url = 'postgresql+psycopg2://user:password@localhost:5432/db_name'
engine = create_engine(connection_url)
© www.soinside.com 2019 - 2024. All rights reserved.