---
title: "pgsql ident鉴权方式实现无密码登录，无需.pgpass"
author: "waiting"
date: "2012-05-24T18:48:03Z"
tags: []
categories: ["数据库"]
slug: "pgsql-ident-pgpass"
draft: false
lastmod: "2012-05-24T18:52:32Z"
id: 1539999
url: "https://waiting.iteye.com/blog/1539999"
---

PostgreSQL 命令行 psql，pg\_dump等不支持口令参数，也就无法像mysql那样无需人工干预（输入口令）执行备份、恢复数据库的命令。

 

网上搜索有多种解决办法，比如通过设置PostgreSQL的环境变量参数来实现保存密码，或者通过Expect 来实现自动交互，帮助输入密码，或者用.pgpass 来保存密码。 参考链接 [http://bbs.chinaunix.net/thread-1760961-1-1.html](http://bbs.chinaunix.net/thread-1760961-1-1.html)

 

这些方法都存在一定的安全隐患。其实pgsql在把安全性搞得如此高同时也提供了简单的解决办法，即pg\_hba.conf中 ident鉴权验证方式

 

修改 pg\_hba.conf 文件，添加到设置开始的第一位

local all all ident map=map1

并把已有的local行注释行为如下结果

#local all all md5

 然后编辑 pg\_ident.conf，添加

map1 root postgres

 然后重启pgsql服务或者reload，如果当前系统用户是root，则可这样直接直接登录数据库

```
psql -Upostgres
```

 其他具有sudo权限的用户也可这样免口令登录

```
sudo /usr/pgsql-9.1/bin/psql -Upostgre
```

 

 

注：pg\_ident.conf 支持正则匹配

参考文档：

[http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html](http://www.postgresql.org/docs/9.1/static/auth-pg-hba-conf.html)

[http://www.postgresql.org/docs/current/static/auth-username-maps.html](http://www.postgresql.org/docs/current/static/auth-username-maps.html)