Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the url of a postgresql database you made in pgadmin?

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.

like image 599
baisbdhfug Avatar asked Oct 26 '19 18:10

baisbdhfug


1 Answers

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.

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

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.

like image 185
Tejas Anil Shah Avatar answered Oct 13 '22 02:10

Tejas Anil Shah