我可以使用以下命令成功创建到Postgres数据库的连接:
my $settings = {
host => 'myhost',
db => 'mydb',
user => 'myuser',
passwd => 'mypasswd'
};
my $connection = DBI->connect(
'DBI:Pg:dbname=' . $settings->{'db'} . ';host=' . $settings->{'host'},
$settings->{'user'},
$settings->{'passwd'},
{
RaiseError => 1,
ShowErrorStatement => 0,
AutoCommit => 0
}
) or die DBI->errstr;
但是我在Perl模块中留下了有值(value)的登录凭据(是的,我更改了它们)。当前,我使用
psql
交互式地发出查询。为了省去记住用户名/密码的麻烦,我将凭据放在了权限为600的文件(〜/.pgpass)中。该文件如下所示:# host:port:database:user:passwd
myhost:5432:mydb:myuser:mypasswd
如何安全使用此文件(
"$ENV{HOME}/.pgpass"
)和DBI
模块隐藏我的凭据?能做到吗什么是最佳做法? 最佳答案
是的!还有是的一种更好的方法。
轻松在测试服务器和实时服务器之间切换。
~/.pgpass
中(用于psql
和pg_dump
)~/.pg_service.conf
(或/etc/pg_service.conf
)中的例如:
#!/usr/bin/perl -T
use strict;
use warnings;
use DBI;
my $dbh = DBI->connect
(
#"dbi:Pg:service=live",
"dbi:Pg:service=test",
undef,
undef,
{
AutoCommit => 0,
RaiseError => 1,
PrintError => 0
}
) or die DBI->errstr;
〜/.pg_service.conf:
# http://www.postgresql.org/docs/9.2/static/libpq-pgservice.html
# /usr/local/share/postgresql/pg_service.conf.sample
# http://search.cpan.org/dist/DBD-Pg/Pg.pm
#
[test]
dbname=hotapp_test
user=hotusr_test
# localhost, no TCP nonsense needed:
host=/tmp
[live]
dbname=hotapp_live
user=hotusr_live
host=pgsql-server.example.org
〜/.pgpass:
# http://www.postgresql.org/docs/9.2/static/libpq-pgpass.html
# hostname:port:database:username:password
localhost:5432:hotapp_test:hotusr_test:kq[O2Px7=g1
pgsql-server.example.org:5432:hotapp_live:hotusr_live:Unm£a7D(H