在 postgres 中创建数据库时出错
我想在linux服务器上创建用户和数据库。 我可以使用以下代码创建用户:
su - postgres
# createuser -S -D -R myUser
但是当我尝试使用代码创建数据库时:
# createdb -U myUser -p 5432 myDatabase
我收到以下错误:
createdb: could not connect to database postgres: FATAL: Ident authentication failed for user "myUser"
我是 Linux 新手,所以我无法弄清楚为什么我能够创建用户但在那里创建数据库postgres 的连接错误。还有身份错误 对用户进行身份验证。
I want to create user and database in linux server.
I am able to create user with the following code:
su - postgres
# createuser -S -D -R myUser
but when I tried to create database with code :
# createdb -U myUser -p 5432 myDatabase
I get following error:
createdb: could not connect to database postgres: FATAL: Ident authentication failed for user "myUser"
I am new to linux so I am unable to figure out why I was able to create user but while creating database there is connection error with postgres. And also error for ident
authentication for user.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ident 是一种依赖于当前登录用户的身份验证模式。如果您对 postgres 使用 su -s 然后尝试以其他用户身份登录,则 ident 将失败(因为它不是当前登录的用户)。您可以通过两种方式解决这个问题,我倾向于使用后者。
解决方案:只需确保当前登录的用户是您想要登录postgres的用户即可:
更好的解决方案:更改pg_hba.conf(可能位于/ etc/postgresql/8.4/main/ 或类似),并确保将“md5”身份验证模式添加到选项列表中。这是我的开发盒上的 pg_hba.conf,没有注释:
这告诉 PostgreSQL postgres 可以使用 ident 登录,所有其他用户可以使用 md5 身份验证登录。这样,您可以使用 psql 二进制文件的 -U 开关来表示您希望成为哪个用户,因此这实际上是有效的:
也就是说,我倾向于使用 postgres 超级用户来创建数据库。然后,我将新创建的数据库的权限授予新创建的用户,如下所示:
希望有帮助。
ADDENDUM:更改 pg_hba.conf 后,您必须重新启动 PostgreSQL 服务器以确保它再次读取配置。您可以通过发出以下命令来执行此操作:
该脚本可能被称为“postgresql”而不是“postgresql-8.4”,具体取决于操作系统和安装方法。
ident is an authentication schema that relies on the currently logged in user. If you've su -s to postgres and then try to login as another user, ident will fail (as it's not the currently logged in user). You can solve this issue in two ways, I tend to use the latter.
solution: simply make sure the currently logged in user is the user with which you would like to log in to postgres:
better solution: change pg_hba.conf (probably located in /etc/postgresql/8.4/main/ or similar), and make sure you add the "md5" authentication schema to the list of options. Here's my pg_hba.conf on my development box, without comments:
This tells PostgreSQL that postgres can login with ident, and all other users can login using the md5 authentication. That way, you can use the -U switch to the psql binary to denote which user you wish to become, so this actually works:
That said, I tend to use the postgres superuser to create databases. I then grant permissions on the newly created database to the newly created user, as such:
Hope that helps.
ADDENDUM: Once you've altered the pg_hba.conf, you will have to restart the PostgreSQL server to make sure it reads the configuration again. You can do so by issuing this command:
The script might be called "postgresql" instead of "postgresql-8.4", depending on OS and method of installation.