PostgreSQL& psycopg2:数据库不存在 [英] Postgresql & psycopg2: database does not exist

查看:328
本文介绍了PostgreSQL& psycopg2:数据库不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试建立与这样的数据库的连接:

I am trying to establish a connection to a database like this:

psycopg2.connect(database = movies,user = lfcj,host = 127.0.0.1);

我的 pg_hba.conf 文件中有一行:

TYPE __ DATABASE___USER__ADDRESS___METHOD

local all lfcj peer

TYPE __ DATABASE___USER__ADDRESS___METHOD
local all lfcj peer

我正在尝试使用对等标识,我的SO用户名也为 lfcj

I am trying to use peer identification, and my SO user name is also lfcj.

当我这样登录postgresql时,将所有特权授予 lfcj ,然后运行 \list

When I log in postgresql like this, grant all privileges to lfcj, and run \list :

psql lfcj -h 127.0.0.1 -d movies 
Enter password:  'myPassword'

psql (9.4.1)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

movies=# GRANT ALL PRIVILEGES ON DATABASE  movies TO lfcj WITH GRANT OPTION;
movies=#\list

我得到此信息:

                            List of databases
Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
--------+----------+----------+-------------+-------------+-----------------------
movies  | lfcj     | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =Tc/lfcj         +
        |          |          |             |             | lfcj=C*T*c*/lfcj+

因此:数据库电影存在, lfcj 是所有者,并拥有所有特权。但是当我运行时:

So: the database movies exists, lfcj is the owner and has all priviliges. But when I run:

psycopg2.connect(movies,lfcj);

它引发错误:

File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL:  database "movies" does not exist

有什么想法吗?

推荐答案

能否尝试连接到 postgres数据库:

Could you try connecting to the "postgres" database:

psql -d postgres

,然后执行以下命令:

and then execute the following command:

show data_directory;

应返回9.3.1.0-alpha1

On 9.3.1.0-alpha1 that should return

/Users/USERNAME/Library/Application Support/Postgres93/var

然后使用速记 \l

检查服务器上存在哪些数据库,似乎Postgresapp成功调用了initdb,但createdb失败。您可以手动执行此操作,只需从终端(不在psql内)执行 createdb USERNAME

If all that works, it seems that Postgresapp called initdb successfully, but createdb failed. You can do that manually, just execute createdb USERNAME from the terminal (not inside psql)

这篇关于PostgreSQL& psycopg2:数据库不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆