Connecting postgresql with sqlalchemy

后端 未结 3 1615
礼貌的吻别
礼貌的吻别 2020-12-12 21:45

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

相关标签:
3条回答
  • 2020-12-12 22:20

    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

    0 讨论(0)
  • 2020-12-12 22:27

    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

    0 讨论(0)
  • 2020-12-12 22:35

    You would need to pip install SQLAlchemy and pip install psycopg2. An example of a SQLAlchemy connection string that uses psycopg2:

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

    You could also connect to your database using the psycopg2 driver exclusively:

    import psycopg2
    conn_string = "host='localhost' dbname='my_database' user='postgres' password='secret'"
    conn = psycopg2.connect(conn_string)
    

    However, using the psycopg2 driver to connect does not take advantage of SQLAlchemy.

    0 讨论(0)
提交回复
热议问题