database Createuser:无法连接到数据库 postgres:致命:角色“tom”不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16973018/
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
Createuser: could not connect to database postgres: FATAL: role "tom" does not exist
提问by Tom Maxwell
I'm trying to set up Postgres for the first time, and I need to create a user with permissions to read and create databases. However, when I use "createuser username" in my terminal I get the following message:
我是第一次尝试设置 Postgres,我需要创建一个具有读取和创建数据库权限的用户。但是,当我在终端中使用“createuser username”时,我收到以下消息:
createuser: could not connect to database postgres: FATAL: role "tom" does not exist
createuser:无法连接到数据库 postgres:致命:角色“tom”不存在
Tom is my Ubuntu user account that I'm logged into right now. I'm trying to create a username of "postgres" then do a "psql -U psql template1" so I can create a database and assign an owner to it for my Rails app.
Tom 是我现在登录的 Ubuntu 用户帐户。我正在尝试创建一个“postgres”的用户名,然后执行一个“psql -U psql template1”,这样我就可以创建一个数据库并为我的 Rails 应用程序分配一个所有者。
Any help?
有什么帮助吗?
回答by Craig Ringer
You mentioned Ubuntu so I'm going to guess you installed the PostgreSQL packages from Ubuntu through apt.
你提到了 Ubuntu,所以我猜你是通过 apt 从 Ubuntu 安装 PostgreSQL 包的。
If so, the postgresPostgreSQL user account already exists and is configured to be accessible via peerauthentication for unix sockets in pg_hba.conf. You get to it by running commands as the postgresunix user, eg:
如果是这样,postgresPostgreSQL用户帐户已经存在,并且被配置为通过访问peer在Unix套接字认证pg_hba.conf。您可以通过以postgresunix 用户身份运行命令来访问它,例如:
sudo -u postgres createuser owning_user
sudo -u postgres createdb -O owning_user dbname
This is all in the Ubuntu PostgreSQLdocumentation that's the first Google hit for "Ubuntu PostgreSQL" and is covered in numerous Stack Overflow questions.
这一切都在Ubuntu PostgreSQL文档中,该文档是“Ubuntu PostgreSQL”的第一个 Google 热门搜索词,并且涵盖了许多 Stack Overflow 问题。
(You've made this question a lot harder to answer by omitting details like the OS and version you're on, how you installed PostgreSQL, etc.)
(通过省略诸如操作系统和版本,您如何安装 PostgreSQL 等详细信息,您使这个问题更难回答。)
回答by user3402754
See git gist with instructions here
Run this:
运行这个:
sudo -u postgres psql
OR
或者
psql -U postgres
in your terminal to get into postgres
在你的终端进入 postgres
NB: If you're on a Mac and both of the commands above failed jump to the section about Mac below
注意:如果您使用的是 Mac 并且上面的两个命令都失败了,请跳转到下面关于 Mac 的部分
postgres=#
Run
跑
CREATE USER new_username;
Note: Replace new_username with the user you want to create, in your case that will be tom.
注意:将 new_username 替换为您要创建的用户,在您的情况下将是 tom。
postgres=# CREATE USER new_username;
CREATE ROLE
Since you want that user to be able to create a DB, you need to alter the role to superuser
由于您希望该用户能够创建数据库,因此您需要将角色更改为超级用户
postgres=# ALTER USER new_username SUPERUSER CREATEDB;
ALTER ROLE
To confirm, everything was successful,
为了确认,一切都成功了,
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
new_username | Superuser, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
root | Superuser, Create role, Create DB | {}
postgres=#
Update/Modification (For Mac):
更新/修改(对于 Mac):
I recently encountered a similar error on my Mac:
我最近在我的 Mac 上遇到了类似的错误:
psql: FATAL: role "postgres" does not exist
psql: FATAL: role "postgres" does not exist
This was because my installation was setup with a database superuser whose role name is the same as your login (short) name.
这是因为我的安装是使用数据库超级用户设置的,其角色名称与您的登录(短)名称相同。
But some linux scripts assume the superuser has the traditional role name of postgres
但是一些 linux 脚本假定超级用户具有传统的角色名称 postgres
How did I resolve this?
我是如何解决这个问题的?
If you installed with homebrewrun:
如果你安装了homebrew运行:
/usr/local/opt/postgres/bin/createuser -s postgres
/usr/local/opt/postgres/bin/createuser -s postgres
If you're using a specific version of postgres, say
10.5then run:
如果您使用的是特定版本的 postgres,请说
10.5然后运行:
/usr/local/Cellar/postgresql/10.5/bin/createuser -s postgres
/usr/local/Cellar/postgresql/10.5/bin/createuser -s postgres
OR:
或者:
/usr/local/Cellar/postgresql/10.5/bin/createuser -s new_username
/usr/local/Cellar/postgresql/10.5/bin/createuser -s new_username
OR:
或者:
/usr/local/opt/postgresql@11/bin/createuser -s postgres
/usr/local/opt/postgresql@11/bin/createuser -s postgres
If you installed with
postgres.appfor Mac run:
如果您安装了
postgres.appfor Mac 运行:
/Applications/Postgres.app/Contents/Versions/10.5/bin/createuser -s postgres
/Applications/Postgres.app/Contents/Versions/10.5/bin/createuser -s postgres
P.S: replace 10.5 with your PostgreSQL version
PS:用你的 PostgreSQL 版本替换 10.5
回答by Tanmay
sudo -u postgres createuser -s tom
this should help you as this will happen if the administrator has not created a PostgreSQL user account for you. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name, in that case you need to use the -U switch.
这应该对您有所帮助,因为如果管理员没有为您创建 PostgreSQL 用户帐户,就会发生这种情况。也可能是您被分配了一个与操作系统用户名不同的 PostgreSQL 用户名,在这种情况下,您需要使用 -U 开关。
回答by Saman Mohamadi
1- Login as default PostgreSQL user (postgres)
1- 以默认 PostgreSQL 用户 (postgres) 登录
sudo -u postgres -i
2- As postgres user. Add a new database user using the createusercommand
2- 作为 postgres 用户。使用createuser命令添加一个新的数据库用户
[postgres]$ createuser --interactive
3-exit
3-出口
[postgres]$ exit
回答by Rocky Inde
Your error is posted in the official documentation. You can read this article.
您的错误已发布在官方文档中。您可以阅读这篇文章。
I have copied the reason for you (and hyperlinked the URLs) from that article:
我从那篇文章中复制了你的原因(并超链接了 URL):
This will happen if the administrator has not created a PostgreSQL user account for you. (PostgreSQL user accounts are distinct from operating system user accounts.) If you are the administrator, see Chapter 20for help creating accounts. You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name; in that case you need to use the -U switch or set the PGUSER environment variable to specify your PostgreSQL user name
如果管理员尚未为您创建 PostgreSQL 用户帐户,则会发生这种情况。(PostgreSQL 用户帐户不同于操作系统用户帐户。)如果您是管理员,请参阅第 20 章以获取创建帐户的帮助。您需要成为安装 PostgreSQL 的操作系统用户(通常是 postgres)才能创建第一个用户帐户。也可能是您被分配了一个与您的操作系统用户名不同的 PostgreSQL 用户名;在这种情况下,您需要使用 -U 开关或设置 PGUSER 环境变量来指定您的 PostgreSQL 用户名
For your purposes, you can do:
为了您的目的,您可以执行以下操作:
1) Create a PostgreSQL user account:
1) 创建一个 PostgreSQL 用户帐户:
sudo -u postgres createuser tom -d -P
(the -Poption to set a password; the -doption for allowing the creation of database for your username 'tom'. Note that 'tom' is your operating system username. That way, you can execute PostgreSQL commands without sudoing.)
(-P设置密码的-d选项;允许为您的用户名“tom”创建数据库的选项。请注意,“tom”是您的操作系统用户名。这样,您无需sudoing即可执行 PostgreSQL 命令。)
2) Now you should be able to execute createdband other PostgreSQL commands.
2) 现在您应该可以执行createdb和其他 PostgreSQL 命令了。
回答by Sagar T
I had the same issue, i just do this
我有同样的问题,我只是这样做
sudo su - postgres
须藤 su - postgres
createuser odoo -U postgres -dRSP #P for password (odooor user namethat you want o give the postgres access)
createuser odoo -U postgres -dRSP #P 用于密码(odoo或您想要授予 postgres 访问权限的用户名)
回答by ostergaard
On Windows use:
在 Windows 上使用:
C:\PostgreSQL\pg10\bin>createuser -U postgres --pwprompt <USER>
C:\PostgreSQL\pg10\bin>createuser -U postgres --pwprompt <USER>
Add --superuseror --createdbas appropriate.
添加--superuser或--createdb视情况而定。
See https://www.postgresql.org/docs/current/static/app-createuser.htmlfor further options.
有关更多选项,请参阅https://www.postgresql.org/docs/current/static/app-createuser.html。
回答by Jean-Marie
You need to first run initdb. It will create the database cluster and the initial setup
您需要先运行 initdb。它将创建数据库集群和初始设置
See How to configure postgresql for the first time?and http://www.postgresql.org/docs/8.4/static/app-initdb.html
请参阅如何首次配置 postgresql?和http://www.postgresql.org/docs/8.4/static/app-initdb.html

