psql:FATAL:数据库“<user>”不存在

本文翻译自:psql: FATAL: database “” does not exist

I'm using the PostgreSql app for mac ( http://postgresapp.com/ ). 我正在使用PostgreSql应用程序的mac( http://postgresapp.com/ )。 I've used it in the past on other machines but it's giving me some trouble when installing on my macbook. 我以前在其他机器上使用它但是在我的macbook上安装时给我带来了一些麻烦。 I've installed the application and I ran: 我安装了应用程序,然后运行:

psql -h localhost

It returns: 它返回:

psql: FATAL:  database "<user>" does not exist

It seems I can't even run the console to create the database that it's attempting to find. 似乎我甚至无法运行控制台来创建它试图找到的数据库。 The same thing happens when I just run: 我跑的时候会发生同样的事情:

psql 

or if I launch psql from the application drop down menu: 或者如果我从应用程序下拉菜单中启动psql:

Machine stats: 机器统计:

  • OSX 10.8.4 OSX 10.8.4

  • psql (PostgreSQL) 9.2.4 psql(PostgreSQL)9.2.4

Any help is appreciated. 任何帮助表示赞赏。

I've also attempted to install PostgreSql via homebrew and I'm getting the same issue. 我也试图通过自制软件安装PostgreSql,我遇到了同样的问题。 I've also read the applications documentation page that states: 我还阅读了应用程序文档页面,其中说明:

When Postgres.app first starts up, it creates the $USER database, which is the default database for psql when none is specified. 当Postgres.app首次启动时,它会创建$ USER数据库,当没有指定时,它是psql的默认数据库。 The default user is $USER, with no password. 默认用户是$ USER,没有密码。

So it would seem the application is not creating $USER however I've installed->uninstalled-reinstalled several times now so it must be something with my machine. 所以似乎应用程序没有创建$ USER但是我已经安装了 - >卸载 - 重新安装了几次,所以它必须是我的机器的东西。

I found the answer but I'm not sure exactly how it works as the user who answered on this thread -> Getting Postgresql Running In Mac: Database "postgres" does not exist didn't follow up. 我找到答案,但我不确定它是如何工作的,因为在这个线程上回答的用户 - > 获取Postgresql在Mac上运行:数据库“postgres”不存在没有跟进。 I used the following command to get psql to open: 我使用以下命令来打开psql:

psql -d template1

I'll leave this one unanswered until someone can provide an explanation for why this works. 我会留下这个答案,直到有人可以解释为什么这样做。


#1楼

参考:https://stackoom.com/question/1BzG2/psql-FATAL-数据库-user-不存在


#2楼

It appears that your package manager failed to create the database named $user for you. 您的包管理器似乎无法为您创建名为$ user的数据库。 The reason that 原因

psql -d template1

works for you is that template1 is a database created by postgres itself, and is present on all installations. 适合你的是template1是由postgres自己创建的数据库,并且存在于所有安装中。 You are apparently able to log in to template1, so you must have some rights assigned to you by the database. 您显然可以登录到template1,因此您必须拥有数据库为您分配的一些权限。 Try this at a shell prompt: 在shell提示符下尝试:

createdb

and then see if you can log in again with 然后看看你是否可以再次登录

psql -h localhost

This will simply create a database for your login user, which I think is what you are looking for. 这将只为您的登录用户创建一个数据库,我认为这是您正在寻找的。 If createdb fails, then you don't have enough rights to make your own database, and you will have to figure out how to fix the homebrew package. 如果createdb失败,那么您没有足够的权限来创建自己的数据库,您将不得不弄清楚如何修复自制软件包。


#3楼

By default, postgres tries to connect to a database with the same name as your user. 默认情况下,postgres尝试连接到与您的用户同名的数据库。 To prevent this default behaviour, just specify user and database: 要防止此默认行为,只需指定用户和数据库:

psql -U Username DatabaseName 

#4楼

在使用自制软件安装postgresql之后我仍然遇到了上述问题 - 我在/ usr / bin之前将/ usr / local / bin放在我的路径中解决了这个问题


#5楼

Since this question is the first in search results, I'll put a different solution for a different problem here anyway, in order not to have a duplicate title. 由于这个问题是搜索结果中的第一个问题,因此无论如何我都会针对不同的问题提出不同的解决方案,以便不会出现重复的标题。

The same error message can come up when running a query file in psql without specifying a database. psql运行查询文件而不指定数据库时,可能会出现相同的错误消息。 Since there is no use statement in postgresql, we have to specify the database on the command line, for example: 由于postgresql中没有use语句,我们必须在命令行中指定数据库,例如:

psql -d db_name -f query_file.sql

#6楼

This error can also occur if the environment variable PGDATABASE is set to the name of a database that does not exist. 如果环境变量PGDATABASE设置为不存在的数据库的名称,也会发生此错误。

On OSX, I saw the following error while trying to launch psql from the Postgress.app menu: 在OSX上,我在尝试从Postgress.app菜单启动psql时看到以下错误:

psql: FATAL: database "otherdb" does not exist

The solution to the error was to remove export PGDATABASE=otherdb from ~/.bash_profile : 该错误的解决方案是从~/.bash_profile删除export PGDATABASE=otherdb

Further, if PGUSER is set to something other than your username, the following error will occur: 此外,如果PGUSER设置为您的用户名以外的其他内容,则会发生以下错误:

psql: FATAL: role "note" does not exist

The solution is to remove export PGUSER=notme from ~/.bash_profile . 解决方案是从~/.bash_profile删除export PGUSER=notme

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值