SQLAlchemy(psycopg2.OperationalError)fe_sendauth:未提供密码

时间:2016-02-09 13:52:36

标签: python database postgresql sqlalchemy

尝试使用sqlalchemy连接到postgres数据库时遇到此身份验证问题。 这就是我的联系方式:

public class Main {

    public static void main(String[] args) {

        SessionFactory sessionFactory = new SessionFactory("com.my.package.name");
        Session session = sessionFactory.openSession();

        Root r = new Root()
        r.addLeaf(new Leaf(), 1);
        r.addLeaf(new Leaf(), 2);
        session.save(r);

        //Until this point everything is alright and
        // all 3 nodes and 2 relationships are created

        String query = "MATCH path = (l1:Leaf)-[*1..100]-(l2:Leaf) WITH path LIMIT 1 RETURN path";
        QueryResultModel qrm = session.query(query, new HashMap<String, Object>());
        // qrm.result.get(0).get("path")  is null
    }
}

这就是我设置db_url = 'postgresql+psycopg2:///<role>:<pass>@localhost:5432/basketball' Engine = create_engine(db_url, echo=False) SessionMaker = ORM.sessionmaker(bind=Engine, autoflush=False) Session = ORM.scoped_session(SessionMaker)

的方式
/etc/postgresql/9.3/main/pg_hba.conf

然后,当访问db时,我得到:

# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     md5
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

1 个答案:

答案 0 :(得分:0)

作为@univerio commented,问题是连接url的结构。

标准的url结构是

dialect+driver://username:password@host:port/database

网址

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

描述了一个名为 <role>:<pass>@localhost:5432/basketball 的 Postgresql 数据库,它没有定义明确的凭据或主机名,因此是错误消息。