简体   繁体   English

在postgres中创建数据库时出错

[英]Got error while creating database in postgres

I want to create user and database in linux server. 我想在linux服务器上创建用户和数据库。 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. 我是linux的新手所以我无法弄清楚为什么我能够创建用户但是在创建数据库时会出现与postgres的连接错误。 And also error for ident authentication for user. 并且还为用户的身份验证错误。

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. 我是linux的新手所以我无法弄清楚为什么我能够创建用户但是在创建数据库时会出现与postgres的连接错误。 And also error for ident authentication for user. 并且还为用户的身份验证错误。

ident is an authentication schema that relies on the currently logged in user. ident是一种依赖于当前登录用户的身份验证模式。 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). 如果你su -s到postgres然后尝试以另一个用户身份登录,则ident将失败(因为它不是当前登录的用户)。 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: 解决方案 :只需确保当前登录的用户是您要登录postgres的用户:

postgres@machine:~$ createuser -S -D -R myUser
postgres@machine:~$ exit
machine:~# su - myUser
myUser@machine:~$ psql myDatabase

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. 更好的解决方案 :更改pg_hba.conf(可能位于/etc/postgresql/8.4/main/或类似),并确保将“md5”身份验证架构添加到选项列表中。 Here's my pg_hba.conf on my development box, without comments: 这是我的开发框中的pg_hba.conf,没有注释:

berry@berry-pc:~$ sudo cat /etc/postgresql/8.4/main/pg_hba.conf | grep -v "^#" | grep -v "^$"
local   all         postgres                          ident
local   all         all                               md5
host    all         all         127.0.0.1/32          md5
host    all         all         ::1/128               md5

This tells PostgreSQL that postgres can login with ident , and all other users can login using the md5 authentication. 这告诉PostgreSQL postgres可以使用ident登录,所有其他用户都可以使用md5身份验证登录。 That way, you can use the -U switch to the psql binary to denote which user you wish to become, so this actually works: 这样,您可以使用-U开关到psql二进制文件来表示您希望成为哪个用户,因此这实际上有效:

berry@berry-pc:~$ psql -U myUser myDatabase.

That said, I tend to use the postgres superuser to create databases. 也就是说,我倾向于使用postgres超级用户来创建数据库。 I then grant permissions on the newly created database to the newly created user, as such: 然后,我将新创建的数据库的权限授予新创建的用户,如下所示:

postgres@debian:~$ psql template1
template1=# CREATE USER myUser WITH PASSWORD 'myPassword';
CREATE ROLE
template1=# CREATE DATABASE myDatabase;
CREATE DATABASE
template1=# GRANT ALL PRIVILEGES ON DATABASE myDatabase TO myUser;
GRANT
template1=# \q

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. 附录 :一旦你改变了pg_hba.conf,就必须重新启动PostgreSQL服务器,以确保它再次读取配置。 You can do so by issuing this command: 您可以通过发出以下命令来执行此操作:

root@debian:~$ /etc/init.d/postgresql-8.4 restart

The script might be called "postgresql" instead of "postgresql-8.4", depending on OS and method of installation. 该脚本可能被称为“postgresql”而不是“postgresql-8.4”,具体取决于操作系统和安装方法。

See for example: this link 例如,请参阅: 此链接

Please use google search for phrase you gave... 请使用谷歌搜索你给的短语...

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

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