我得到了错误:

FATAL: Peer authentication failed for user "postgres"

当我尝试让postgres与Rails一起工作时。

这是pg_hba.conf,我的数据库。Yml,以及完整跟踪的转储。

我在pg_hba中将身份验证更改为md5,并尝试了不同的方法,但似乎都不起作用。

我还尝试按照Rails 3.2创建一个新用户和数据库,FATAL: Peer authentication failed for user (PG::Error)

但是它们不会在pgadmin上显示,甚至当我运行sudo -u postgres psql -l时也不会显示。

知道我哪里错了吗?


当前回答

许多其他答案与各种配置文件中的设置有关,与pg_hba.conf有关的答案确实适用,并且是100%正确的。但是,请确保您正在修改正确的配置文件。

正如其他人所提到的,配置文件的位置可以在主配置文件中使用各种设置来覆盖,也可以在命令行上使用-D选项提供主配置文件的路径。

您可以在psql会话中使用以下命令来显示正在读取配置文件的位置(假设您可以启动psql)。这只是一个故障排除步骤,可以帮助一些人:

select * from pg_settings where setting~'pgsql';  

您还应该确保postgres用户的主目录位于您预期的位置。我这样说是因为很容易忽略这一点,因为您的提示符将显示'~'而不是您的主目录的实际路径,这使得它不那么明显。许多安装默认postgres用户主目录为/var/lib/pgsql.

如果它没有被设置为它应该是什么,停止postgresql服务,并在以root身份登录时使用以下命令。同时确保postgres用户没有登录到另一个会话:

usermod -d /path/pgsql postgres

最后,通过输入echo $PGDATA来确保你的PGDATA变量设置正确,它应该输出类似于:

/path/pgsql/data

如果它没有设置,或者显示的内容与您期望的不同,请检查您的启动文件或RC文件,如.profile或.bash。rc -这将根据您的操作系统和shell而有很大的不同。一旦你为你的机器确定了正确的启动脚本,你可以插入以下内容:

export PGDATA=/path/pgsql/data

对于我的系统,我把它放在/etc/profile.d/profile.local.sh中,这样所有用户都可以访问它。

现在您应该能够像往常一样初始化数据库,所有的psql路径设置都应该是正确的!

其他回答

在连接中使用host=localhost。

PGconn *conn = PQconnectdb(
    "host=localhost user=postgres dbname=postgres password=123"
);

我也有同样的问题。

depa的解决方案是绝对正确的。

只要确保你有一个用户配置使用PostgreSQL。

检查文件:

$ ls /etc/postgresql/9.1/main/pg_hba.conf -l

这个文件的权限应该给你注册psql的用户。

进一步。如果你到现在还好…

按照@depa的指示更新。

i.e.

$ sudo nano /etc/postgresql/9.1/main/pg_hba.conf

然后做出改变。

如果您通过localhost(127.0.0.1)连接,则不应该遇到这个特定问题。我不会对pg_hba.conf做太多的改动,但我会调整你的连接字符串:

psql -U someuser -h 127.0.0.1 database

其中someuser是你连接的用户,database是你的用户有权限连接的数据库。

下面是我在Debian上设置postgres的方法:

http://www.postgresql.org/download/linux/debian/  (Wheezy 7.x)

as root …

    root@www0:~# echo "deb http://apt.postgresql.org/pub/repos/apt/ wheezy-pgdg main" >> /etc/apt/sources.list

    root@www0:~# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -

    root@www0:~# apt-get update

    root@www0:~# apt-get install postgresql-9.4        

    root@www0:~# su - postgres 

    postgres@www0:~$ createuser --interactive -P someuser
    Enter password for new role:
    Enter it again:
    Shall the new role be a superuser? (y/n) n
    Shall the new role be allowed to create databases? (y/n) y
    Shall the new role be allowed to create more new roles? (y/n) n

    postgres@www0:~$ createdb -O someuser database

    postgres@www0:~$ psql -U someuser -h 127.0.0.1 database

享受吧!

如果您有问题,您需要找到pg_hba.conf。命令如下:

查找/ -name 'pg_hba.conf' 2>/dev/null

然后修改配置文件:

Postgresql 9.3

Postgresql 9.4

下一步是:重新启动你的db实例:

Service postgresql-9.3 restart

如果您有任何问题,您需要重新设置密码:

修改用户密码为db_password

pg_config用于遵从性信息,帮助扩展和客户端程序根据PostgreSQL编译和链接。它对机器上的活动PostgreSQL实例一无所知,只知道二进制文件。

pg_hba.conf可以出现在许多其他位置,这取决于Pg是如何安装的。标准的位置是数据库data_directory中的pg_hbase .conf(可以在/home, /var/lib/pgsql, /var/lib/postgresql/[version]/, /opt/postgres/,等等等等),但是用户和打包者可以把它放在他们喜欢的任何地方。不幸的是。

找到pg_hba.conf的唯一有效方法是询问正在运行的PostgreSQL实例的pg_hba.conf在哪里,或者询问系统管理员它在哪里。你甚至不能依赖于询问datadir在哪里并解析postgresql.conf,因为init脚本在启动Pg时可能会传递一个参数,比如-c hba_file=/some/other/path。

你要做的是问PostgreSQL:

SHOW hba_file;

这个命令必须在超级用户会话上运行,所以对于shell脚本来说,你可以这样写:

psql -t -P format=unaligned -c 'show hba_file';

并设置环境变量“PGUSER”、“PGDATABASE”等,确保连接正确。

是的,这有点像鸡生蛋还是蛋生鸡的问题,因为如果用户无法连接(比如,在搞砸了编辑pg_hba.conf之后),你就无法找到pg_hba.conf来修复它。

另一个选项是查看ps命令的输出,看看postmaster data目录参数-D是否可见。

ps aux  | grep 'postgres *-D'

因为pg_hba.conf将在data目录中(除非你在Debian/Ubuntu或一些衍生版本上使用他们的包)。

如果你的目标是Ubuntu系统,PostgreSQL是从Debian/Ubuntu包中安装的,这就容易一些了。你不需要处理从源文件手工编译的Pg,比如有人在他们的主目录下initdb了一个datadir,或者在/opt目录下安装了一个EnterpriseDB Pg,等等。你可以询问pg_wrapper, Debian/Ubuntu多版本Pg管理器,其中PostgreSQL正在使用pg_wrapper中的pg_lsclusters命令。

If you can't connect (Pg isn't running, or you need to edit pg_hba.conf to connect) you'll have to search the system for pg_hba.conf files. On Mac and Linux something like sudo find / -type f -name pg_hba.conf will do. Then check the PG_VERSION file in the same directory to make sure it's the right PostgreSQL version if you have more than one. (If pg_hba.conf is in /etc/, ignore this, it's the parent directory name instead). If you have more than one data directory for the same PostgreSQL version you'll have to look at database size, check the command line of the running postgres from ps to see if it's data directory -D argument matches where you're editing, etc. https://askubuntu.com/questions/256534/how-do-i-find-the-path-to-pg-hba-conf-from-the-shell/256711