SqlAlchemy - PostgreSQL - 会话连接错误的信息

时间:2016-02-09 17:14:43

标签: python postgresql sqlalchemy

我编写了以下代码来连接数据库并填充架构:

db_url = 'postgresql+psycopg2:///bidder:<pass>@localhost:5432/basketball'
Engine = create_engine(db_url, echo=False)
SessionMaker = ORM.sessionmaker(bind=Engine, autoflush=False)
Session = ORM.scoped_session(SessionMaker)
Base.metadata.create_all(Engine)

然而,最后一句话提出了:

(psycopg2.OperationalError) FATAL:  role "fran" does not exist

(“fran”是我的unix用户名)

Sqlalchemy没有使用我在db_url中指定的用户名和密码连接到数据库。

List of databases
    Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
------------+----------+----------+-------------+-------------+-----------------------
 basketball | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres         +
            |          |          |             |             | postgres=CTc/postgres+
            |          |          |             |             | bidder=CTc/postgres
 postgres   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres
 template1  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
            |          |          |             |             | postgres=CTc/postgres

用户表:

 rolname  
----------
 postgres
 bidder
(2 rows)

1 个答案:

答案 0 :(得分:1)

尝试从db_url中删除/之一。

db_url = 'postgresql+psycopg2://bidder:<pass>@localhost:5432/basketball'