2

I try to connect a PostgreSQL database with SQLAlchemy.

I created a new role like that.First of all I logged into the postgres account with the command

sudo -i -u postgres

Next, issued the command:

createuser --interactive

Also I created a new user account for database users.

sudo adduser veritabani

Once I've completed this process, I used this command to access the PostgreSQL prompt

sudo -i -u veritabani
psql -d havalimani

Also I executed an SQL stament which works fine.

SELECT * FROM uçuşlar;

On the other hand I'd like to access database using Python and SQLAlchemy. I have a code like that :

import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

def main():
    print (os.getenv("DATABASE_URL"))

    flights =  db.execute("SELECT kalkış, varış, süre FROM uçuşlar")
    for flight in flights:
        print(f"{flight.kalkış} dan {flight.varış} a, {flight.süre} dakika")

if __name__ == "__main__":
    main()

I use this statement to give DATABASE_URL an initial value;

export DATABASE_URL="postgresql://localhost/havalimani?user=veritabani&password=pwd"

But when I run Python program it gives an error like that :

FATAL:  password authentication failed for user "veritabani" 
(Background on this error at: http://sqlalche.me/e/e3q8)
  • Finally it works. I am not sure how it works. I didn't understand why authentication failed before. This post helped me : https://stackoverflow.com/a/12721020/762630 –  Nov 05 '18 at 13:18

0 Answers0