so i created a postgresql database and i want to link it to my python code using sqlalchemy but since i made the database in pgadmin i dont know what to mention as the url of the database in the code. thank you in advance.
If you created your database using the defaults, you can probably login to the server using psql (SQL Shell).
Try to connect to the database you created and if you are able to connect to the database try \conninfo
.
If it's on your own machine the output will be quite standard host will be localhost
, ip will be 127.0.0.1
and port will be the default port of 5432
.
Once you make sure of these things you should try to connect to the database using the following code from this answer to a different question .
Please make sure you have both SQLAlchemy and psycopg2 installed before you try to connect. Then try this:
from sqlalchemy import create_engine
engine = create_engine('postgresql+psycopg2://user:password@hostname/database_name')
Or may be find a good tutorial on SQLAlchemy.
Go back into pgAdmin, click on the node for your server and then check the values under "Connection" in the "Properties" page:
According to the SQLAlchemy documentation the corresponding connection URL for those values would be
connection_url = 'postgresql+psycopg2://user:password@localhost:5432/db_name'
engine = create_engine(connection_url)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.