PostgreSQL DB connection免输入密码连接PG

作者:moocbaby(handan)

日期:2019-01-19

标签:postgreSQL, connection

Linux环境下,PostgreSQL DB连接令如下,

[postgres@hname~]psql -U username -h 20.100.53.221 -p 5432 -d opdb

enter passwd for username: (这里输入密码)

-U 用户名

-h DB host IP

-p Port号

-d 数据库名

我们在写shell脚本时会希望免输入密码,有以下三种方法。

方法1:

export PGPASSWORD

[postgres@hname~]export PGPASSWORD=pass

[postgres@hname~]psql -U username -h 20.100.53.221 -p 5432 -d opdb

方法2:

在/home/postgres 目录下新增 .pgpass 隐藏密码文件,可以写个数据连接信息,分行即可,格式如下

#DBhostIP:port:DBname:user:passowrd

10.224.20.4:5432:testdb:user:pass

20.100.53.221:5432:opdb:username:pass

保存文件后,运行一下文件免输入密码:

[postgres@hname~]psql -U username -h 20.100.53.221 -p 5432 -d opdb

方法3:

将密码写到命令行里,如下

[postgres@hname~]psql “host=20.100.53.221 port=5432 user=username password=pass dbname=opdb”

参考:psql --help 命令可查看数据库连接帮助文档

原文地址:https://github.com/moocbaby/postgresql/wiki/PostgreSQL-DB-connection%E5%85%8D%E8%BE%93%E5%85%A5%E5%AF%86%E7%A0%81%E8%BF%9E%E6%8E%A5PG

猜你喜欢

转载自blog.csdn.net/handan725/article/details/86549617
今日推荐