postgresql 如何让 pg_dump 正确进行身份验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10430645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:29:13  来源:igfitidea点击:

How can I get pg_dump to authenticate properly

postgresqlpg-dump

提问by Kosmonaut

I have tried using host variable PGPASSWORDand .pgpassand neither of these two will allow me to authenticate to the database. I have chmod'd .pgpassto appropriate permissions and also tried:

我一直在使用主机变量尝试PGPASSWORD.pgpass并没有这两个让我来验证数据库。我有chmod“d.pgpass适当的权限,并也尝试:

export PGPASSWORD=mypass and PGPASSWORD=mypass

The password DOES contain a \however I was encasing it in single quotes PGPASS='mypass\'and it still will not authenticate.

密码确实包含一个,\但是我用单引号将它括起来PGPASS='mypass\',它仍然不会进行身份验证。

I'm running:

我在跑:

pg_dump dbname -U username -Fc

and I still receive

我仍然收到

pg_dump: [archiver (db)] connection to database "dbname" failed: FATAL:  Peer authentication failed for user "username"

回答by Jim Mitchener

The Quick Solution

快速解决方案

The problem is that it's trying to perform local peerauthentication based on your current username. If you would like to use a password you must specify the hostname with -h.

问题是它试图peer根据您当前的用户名执行本地身份验证。如果您想使用密码,您必须使用-h.

pg_dump dbname -U username -h localhost -F c

Explanation

解释

This is due to the following in your pg_hba.conf

这是由于您的以下内容 pg_hba.conf

local   all             all                                     peer
host    all             all             127.0.0.1/32            md5

This tells Postgres to use peerauthentication for local users which requires the postgres username to match your current system username. The second line refers to connections using a hostname and will allow you to authenticate with a password via the md5method.

这告诉 Postgrespeer对本地用户使用身份验证,这需要 postgres 用户名与您当前的系统用户名匹配。第二行是指使用主机名的连接,并允许您通过该md5方法使用密码进行身份验证。

My Preferred Development Config

我的首选开发配置

NOTE: This should only be used on single-user workstations. This could lead to a major security vulnerability on a production or multi-user machine.

注意:这应该只用于单用户工作站。这可能会导致生产或多用户计算机上的重大安全漏洞。

When developing against a local postgres instance I like to change my local authentication method to trust. This will allow connecting to postgres via a local unix socket as any user with no password. It can be done by simply changing peerabove to trustand reloading postgres.

在针对本地 postgres 实例进行开发时,我喜欢将本地身份验证方法更改为trust. 这将允许作为没有密码的任何用户通过本地 unix 套接字连接到 postgres。它可以通过简单地将peer上面更改为trust并重新加载 postgres来完成。

# Don't require a password for local connections
local   all             all                                     trust

回答by Roland Pihlakas

Sometimes you can use

有时你可以使用

sudo -u postgres pg_dump ...