我得到了错误:

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时也不会显示。

知道我哪里错了吗?


当前回答

下面的命令适用于我:

psql -d myDb -U username -W

其他回答

如果您通过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

享受吧!

sudo psql --host=localhost --dbname=database-name --username=postgres

这解决了我的问题

在CentOS 7, PG 10上,文件路径为

/var/lib/pgsql/10/data/pg_hba.conf

上面的编辑对我来说是有效的,因为我发现在做了这些编辑之后我需要重新启动postgres服务器。 ubuntu的:

sudo /etc/init.d/postgresql restart

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