postgresql psql:致命:数据库“<用户>”不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17633422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:16:42  来源:igfitidea点击:

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

postgresqlpsql

提问by Ryan Rich

I'm using the PostgreSql app for 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. I've installed the application and I ran:

我正在使用适用于 Mac 的 PostgreSql 应用程序 ( http://postgresapp.com/)。我过去在其他机器上使用过它,但在我的 macbook 上安装时给我带来了一些麻烦。我已经安装了该应用程序并运行了:

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

  • psql (PostgreSQL) 9.2.4

  • OSX 10.8.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. I've also read the applications documentation page that states:

我还尝试通过自制软件安装 PostgreSql,但遇到了同样的问题。我还阅读了应用程序文档页面,其中指出:

When Postgres.app first starts up, it creates the $USER database, which is the default database for psql when none is specified. The default user is $USER, with no password.

当 Postgres.app 首次启动时,它会创建 $USER 数据库,当没有指定时,它是 psql 的默认数据库。默认用户是 $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 existdidn't follow up. I used the following command to get psql to open:

我找到了答案,但我不确定它是如何工作的,因为在此线程上回答的用户 ->在 Mac 中运行 Postgresql:数据库“postgres”不存在没有跟进。我使用以下命令打开 psql:

psql -d template1

I'll leave this one unanswered until someone can provide an explanation for why this works.

我不会回答这个问题,直到有人可以解释为什么会这样。

回答by Kirk Roybal

It appears that your package manager failed to create the database named $user for you. The reason that

您的包管理器似乎未能为您创建名为 $user 的数据库。原因是

psql -d template1

works for you is that template1 is a database created by postgres itself, and is present on all installations. You are apparently able to log in to template1, so you must have some rights assigned to you by the database. Try this at a shell prompt:

对您有用的是 template1 是由 postgres 本身创建的数据库,并且存在于所有安装中。您显然能够登录到 template1,因此您必须拥有数据库分配给您的某些权限。在 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 失败,那么您没有足够的权限来创建自己的数据库,您将不得不弄清楚如何修复 homebrew 包。

回答by Dhananjay

From the terminal, just Run the command on your command prompt window. (Not inside psql).

在终端中,只需在命令提示符窗口中运行该命令。(不在 psql 中)

createdb <user>

And then try to run postgres again.

然后再次尝试运行 postgres。

回答by Aneer Dev

By default, postgres tries to connect to a database with the same name as your user. To prevent this default behaviour, just specify user and database:

默认情况下,postgres 尝试连接到与您的用户同名的数据库。为了防止这种默认行为,只需指定用户和数据库:

psql -U Username DatabaseName 

回答by user286539

  1. Login as default user: sudo -i -u postgres
  2. Create new User: createuser --interactive
  3. When prompted for role name, enter linux username, and select Yes to superuser question.
  4. Still logged in as postgres user, create a database: createdb <username_from_step_3>
  5. Confirm error(s) are gone by entering: psqlat the command prompt.
  6. Output should show psql (x.x.x) Type "help" for help.
  1. 以默认用户身份登录: sudo -i -u postgres
  2. 创建新用户: createuser --interactive
  3. 当提示输入角色名称时,输入 linux 用户名,然后选择是超级用户问题。
  4. 仍然以 postgres 用户身份登录,创建一个数据库: createdb <username_from_step_3>
  5. 通过psql在命令提示符下输入:确认错误已消失。
  6. 输出应显示 psql (x.x.x) Type "help" for help.

回答by gogasca

Login using default template1 database:

使用默认的 template1 数据库登录:

#psql -d template1
#template1=# \l

  List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-----------+---------+----------+-------------+-------------+---------------------
 postgres  | gogasca | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | gogasca | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca         +
           |         |          |             |             | gogasca=CTc/gogasca
 template1 | gogasca | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca         +
           |         |          |             |             | gogasca=CTc/gogasca
(3 rows)

Create a database with your userId:

使用您的用户 ID 创建一个数据库:

template1=# CREATE DATABASE gogasca WITH OWNER gogasca ENCODING 'UTF8';
CREATE DATABASE

Quit and then login again

退出然后重新登录

template1=# \q
gonzo:~ gogasca$ psql -h localhost
psql (9.4.0)
Type "help" for help.

gogasca=# \l
                                List of databases
   Name    |  Owner  | Encoding |   Collate   |    Ctype    |  Access privileges  
-----------+---------+----------+-------------+-------------+---------------------
 gogasca   | gogasca | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | gogasca | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | gogasca | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca         +
           |         |          |             |             | gogasca=CTc/gogasca
 template1 | gogasca | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/gogasca         +
           |         |          |             |             | gogasca=CTc/gogasca
(4 rows)

回答by Dinesh Pallapa

I faced the same error when I trying to open postgresql on mac

当我尝试在 mac 上打开 postgresql 时,我遇到了同样的错误

psql: FATAL:  database "user" does not exist

I found this simple command to solve it:

我找到了这个简单的命令来解决它:

method1

方法一

$ createdb --owner=postgres --encoding=utf8 user

and type

并输入

 psql

Method 2:

方法二:

psql -d postgres

回答by rust

Had the same problem, a simple psql -d postgresdid it (Type the command in the terminal)

有同样的问题,一个简单的psql -d postgres做到了(在终端中输入命令)

回答by sdhca

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

psql: FATAL: database "otherdb" does not exist

The solution to the error was to remove export PGDATABASE=otherdbfrom ~/.bash_profile:

错误的解决方案是export PGDATABASE=otherdb~/.bash_profile以下位置删除:

Further, if PGUSER is set to something other than your username, the following error will occur:

此外,如果 PGUSER 设置为您的用户名以外的其他内容,则会出现以下错误:

psql: FATAL: role "note" does not exist

psql: FATAL: role "note" does not exist

The solution is to remove export PGUSER=notmefrom ~/.bash_profile.

解决方案是export PGUSER=notme~/.bash_profile.

回答by user686249

As the createdb documentationstates:

正如createdb 文档所述:

The first database is always created by the initdb command when the data storage area is initialized... This database is called postgres.

第一个数据库总是在数据存储区初始化时通过initdb命令创建的……这个数据库叫做postgres。

So if certain OS/postgresql distributions do that differently, it is certainly not the default/standard (just verified that initdbon openSUSE 13.1 creates the DB "postgres", but not "<user>"). Long story short, psql -d postgresis expected to be used when using a user other than "postgres".

因此,如果某些 OS/postgresql 发行版以不同方式执行此操作,则它肯定不是默认/标准(只是验证了initdb在 openSUSE 13.1 上创建了数据库“postgres”,而不是“<user>”)。长话短说,psql -d postgres预计在使用“postgres”以外的用户时使用。

Obviously the accepted answer, running createdbto create a DB named like the user, works as well, but creates a superfluous DB.

显然接受的答案,运行createdb以创建一个像用户一样命名的数据库,也可以工作,但会创建一个多余的数据库。

回答by Babken Vardanyan

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 psqlwithout specifying a database. Since there is no usestatement in postgresql, we have to specify the database on the command line, for example:

psql未指定数据库的情况下运行查询文件时,可能会出现相同的错误消息。由于usepostgresql中没有语句,我们必须在命令行中指定数据库,例如:

psql -d db_name -f query_file.sql