简体   繁体   English

Createuser:无法连接到数据库 postgres:致命:角色“tom”不存在

[英]Createuser: could not connect to database postgres: FATAL: role "tom" does not exist

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.我第一次尝试设置 Postgres,我需要创建一个具有读取和创建数据库权限的用户。 However, when I use:但是,当我使用:

createuser username

in my terminal I get the following message:在我的终端中,我收到以下消息:

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. Tom 是我现在登录的 Ubuntu 用户帐户。 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.我正在尝试创建一个“postgres”的用户名,然后执行psql -U psql template1以便我可以创建一个数据库并为我的 Rails 应用程序分配一个所有者。

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 postgres PostgreSQL user account already exists and is configured to be accessible via peer authentication for unix sockets in pg_hba.conf .如果是这样,则postgres PostgreSQL 用户帐户已经存在,并且配置为可通过pg_hba.conf unix 套接字的peer身份验证进行访问。 You get to it by running commands as the postgres unix user, eg:您可以通过以postgres unix 用户身份运行命令来访问它,例如:

sudo -u postgres createuser owning_user
sudo -u postgres createdb -O owning_user dbname

This is all in the Ubuntu PostgreSQL documentation 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 等详细信息,您使这个问题更难回答。)

See git gist with instructions here 在此处查看 git gist 和说明

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

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 homebrew run:如果您安装了homebrew运行:

/usr/local/opt/postgres/bin/createuser -s postgres

If you're using a specific version of postgres, say 10.5 then run:如果您使用的是特定版本的 postgres,请说10.5然后运行:

/usr/local/Cellar/postgresql/10.5/bin/createuser -s postgres

OR:要么:

/usr/local/Cellar/postgresql/10.5/bin/createuser -s new_username

OR:要么:

/usr/local/opt/postgresql@11/bin/createuser -s postgres

If you installed with postgres.app for Mac run:如果你安装了postgres.app for Mac 运行:

/Applications/Postgres.app/Contents/Versions/10.5/bin/createuser -s postgres

PS: replace 10.5 with your PostgreSQL version PS:用你的 PostgreSQL 版本替换 10.5

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.这应该对您有所帮助,因为如果管理员没有为您创建 PostgreSQL 用户帐户,就会发生这种情况。 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 用户名,在这种情况下,您需要使用 -U 开关。

1- Login as default PostgreSQL user (postgres) 1- 以默认 PostgreSQL 用户 (postgres) 登录

sudo -u postgres -i

2- As postgres user. 2- 作为 postgres 用户。 Add a new database user using the createuser command使用createuser命令添加一个新的数据库用户

[postgres]$ createuser --interactive

3-exit 3-出口

[postgres]$ exit

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 用户帐户,则会发生这种情况。 (PostgreSQL user accounts are distinct from operating system user accounts.) If you are the administrator, see Chapter 20 for help creating accounts. (PostgreSQL 用户帐户不同于操作系统用户帐户。)如果您是管理员,请参阅第 20 章以获取创建帐户的帮助。 You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account.您需要成为安装 PostgreSQL 的操作系统用户(通常是 postgres)才能创建第一个用户帐户。 It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name;也可能是您被分配了一个与您的操作系统用户名不同的 PostgreSQL 用户名; in that case you need to use the -U switch or set the PGUSER environment variable to specify your PostgreSQL user name在这种情况下,您需要使用 -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 -P option to set a password; the -d option 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 sudo ing.) (设置密码的-P选项;允许为您的用户名 'tom' 创建数据库的-d选项。请注意,'tom' 是您的操作系统用户名。这样,您可以执行 PostgreSQL 命令而无需sudo ing。 )

2) Now you should be able to execute createdb and other PostgreSQL commands. 2) 现在您应该可以执行createdb和其他 PostgreSQL 命令了。

I had the same issue, i just do this我有同样的问题,我只是这样做

sudo su - postgres须藤 su - postgres

createuser odoo -U postgres -dRSP #P for password ( odoo or user name that you want o give the postgres access) createuser odoo -U postgres -dRSP #P 用于密码( odoo或您想要授予 postgres 访问权限的用户名

On Windows use:在 Windows 上使用:

C:\\PostgreSQL\\pg10\\bin>createuser -U postgres --pwprompt <USER>

Add --superuser or --createdb as appropriate.根据需要添加--superuser--createdb

See https://www.postgresql.org/docs/current/static/app-createuser.html for further options.有关更多选项,请参阅https://www.postgresql.org/docs/current/static/app-createuser.html

If you don't want to change the authentication method (ident) and mess with pg_hba.conf use this:如果您不想更改身份验证方法 (ident) 并弄乱pg_hba.conf,请使用以下命令:

First login as the default user以默认用户身份首次登录

 sudo su - postgres

then access psql and create a user with the same name as the one you are login in然后访问 psql 并创建一个与您登录的用户同名的用户

postgres=# CREATE USER userOS WITH PASSWORD 'garbage' CREATEDB;

you can verify your user with the corresponding roles with您可以使用相应的角色验证您的用户

postgres=#  \du

Afer this you can create your database and verify it with在此之后,您可以创建您的数据库并验证它

psql -d dbName
\l
\q

For Linux, you can activate SUPERUSER to enable to create databases like this:对于 Linux,您可以激活 SUPERUSER 以启用创建数据库,如下所示:

  1. Go to terminal/shell Go 到端子/外壳

  2. Enter inside postgres using sudo -u postgres -i使用sudo -u postgres -i进入 postgres

Now your terminal will be like "postgres@anish-Latitude-E7450:~$"现在你的终端就像“postgres@anish-Latitude-E7450:~$”

  1. Enter postgres terminal using psql使用psql进入 postgres 终端

Now your terminal will be like "postgres=#"现在你的终端就像“postgres=#”

  1. Enter alter user [postgres_user_name] createdb;输入alter user [postgres_user_name] createdb;

Now the user will be having access to create db.现在用户将有权创建数据库。

  1. Exit psql using '\q'使用 '\q' 退出 psql

  2. Logout postgres using exit使用exit注销 postgres

Hope this helps you.希望这对您有所帮助。

You need to first run initdb.您需要先运行 initdb。 It will create the database cluster and the initial setup它将创建数据库集群和初始设置

See How to configure postgresql for the first time?请参阅如何首次配置 postgresql? and http://www.postgresql.org/docs/8.4/static/app-initdb.htmlhttp://www.postgresql.org/docs/8.4/static/app-initdb.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM