Skip to content

Commit 12c250f

Browse files
authored
add support for PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD and ~/… (#17)
* add support for PGHOST, PGPORT, PGDATABASE, PGUSER, PGPASSWORD and ~/.pgpass
1 parent a05850e commit 12c250f

File tree

3 files changed

+102
-10
lines changed

3 files changed

+102
-10
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ install:
2525
before_script:
2626
- psql -c 'create database travis_ci_test;' -U postgres
2727

28-
script: "./postgresqltuner.pl --host=localhost --user=postgres --database=travis_ci_test"
28+
script: "./postgresqltuner.pl --host=localhost --user=postgres --database=travis_ci_test --password=''"
2929

3030
notifications:
3131
recipients:

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ postgresqltuner.pl --host=dbhost --database=testdb --user=username --password=qw
145145
postgres$ postgresqltuner.pl --host=/var/run/postgresql # PostgreSQL socket directory
146146
```
147147

148+
If available postgresqltuner.pl will use standard PostgreSQL variables like `PGHOST`, `PGPORT`, `PGDATABASE`, `PGUSERNAME`, and password from `~/.pgpass` file.
149+
150+
148151
### With docker
149152

150153
- Via network :
@@ -164,6 +167,13 @@ docker run -it --rm --link your-postgresql-container:dbhost jfcoz/postgresqltune
164167

165168
When using it remotly, postgresqltuner.pl will use ssh to collect OS informations. You must configure ssh to connect to remote host with private key authentication.
166169

170+
### Passwords
171+
172+
For better security use a `~/.pgpass` file containing passwords, so password will not be saved in the shell history nor in the process list. [.pgpass documentation](https://www.postgresql.org/docs/current/static/libpq-pgpass.html)
173+
```
174+
host:port:database:username:password
175+
```
176+
167177
## Options
168178

169179
- Average number of work_mem buffer per connection :

postgresqltuner.pl

Lines changed: 91 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,26 +47,27 @@
4747
exit 1;
4848
}
4949

50-
my $script_version="0.0.10";
50+
my $script_version="0.0.11";
5151
my $script_name="postgresqltuner.pl";
5252
my $min_s=60;
5353
my $hour_s=60*$min_s;
5454
my $day_s=24*$hour_s;
5555
my $os_cmd_prefix='';
5656

57-
my $host='/var/run/postgresql';
58-
my $username='postgres';
59-
my $password='';
60-
my $database="template1";
61-
my $port=5432;
57+
my $host=undef;
58+
my $username=undef;
59+
my $password=undef;
60+
my $database=undef;
61+
my $port=undef;
62+
my $pgpassfile=$ENV{HOME}.'/.pgpass';
6263
my $help=0;
6364
my $work_mem_per_connection_percent=150;
6465
GetOptions (
6566
"host=s" => \$host,
6667
"user=s" => \$username,
6768
"username=s" => \$username,
68-
"pass=s" => \$password,
69-
"password=s" => \$password,
69+
"pass:s" => \$password,
70+
"password:s" => \$password,
7071
"db=s" => \$database,
7172
"database=s" => \$database,
7273
"port=i" => \$port,
@@ -79,12 +80,78 @@
7980
usage(0);
8081
}
8182

83+
# host
84+
if (!defined($host)) {
85+
if (defined($ENV{PGHOST})) {
86+
$host=$ENV{PGHOST};
87+
} else {
88+
$host='/var/run/postgresql';
89+
}
90+
}
91+
92+
# port
93+
if (!defined($port)) {
94+
if (defined($ENV{PGPORT})) {
95+
$port=$ENV{PGPORT};
96+
} else {
97+
$port=5432;
98+
}
99+
}
100+
101+
# database
102+
if (!defined($database)) {
103+
if (defined($ENV{PGDATABASE})) {
104+
$database=$ENV{PGDATABASE};
105+
} else {
106+
$database='template1';
107+
}
108+
}
109+
110+
# user
111+
if (!defined($username)) {
112+
if (defined($ENV{PGUSER})) {
113+
$username=$ENV{PGUSER};
114+
} else {
115+
$username='postgres';
116+
}
117+
}
118+
119+
# if needed, get password from ~/.pgpass
120+
if (!defined($password)) {
121+
if (defined($ENV{PGPASSWORD})) {
122+
$password=$ENV{PGPASSWORD};
123+
} else {
124+
if (defined($ENV{PGPASSFILE})) {
125+
$pgpassfile=$ENV{PGPASSFILE};
126+
}
127+
}
128+
129+
if (open(PGPASS,'<',$pgpassfile)) {
130+
while (my $line=<PGPASS>) {
131+
chomp($line);
132+
next if $line =~ /^\s*#/;
133+
my ($pgp_host,$pgp_port,$pgp_database,$pgp_username,$pgp_password,$pgp_more)=split(/(?<!\\):/,$line); # split except after escape char
134+
next if (!defined($pgp_password) or defined($pgp_more)); # skip malformated line
135+
next if (!pgpass_match('host',$host,$pgp_host));
136+
next if (!pgpass_match('port',$port,$pgp_port));
137+
next if (!pgpass_match('database',$database,$pgp_database));
138+
next if (!pgpass_match('username',$username,$pgp_username));
139+
$password=pgpass_unescape($pgp_password);
140+
last;
141+
}
142+
close(PGPASS);
143+
}
144+
}
145+
82146
usage(1) if (!defined($host) or !defined($username) or !defined($password));
83147

84148
sub usage {
85149
my $return=shift;
86150
print STDERR "usage: $script_name --host [ hostname | /var/run/postgresql ] [--user username] [--password password] [--database database] [--port port] [--wmp 150]\n";
87-
print STDERR "\twmp: average number of work_mem buffers per connection in percent (default 150)\n";
151+
print STDERR "If available connection informations can be read from \$PGHOST, \$PGPORT, \$PGDATABASE, \$PGUSER, \$PGPASSWORD\n";
152+
print STDERR "For security reasons, prefer usage of password in ~/.pgpass\n";
153+
print STDERR "\thost:port:database:username:password\n";
154+
print STDERR " --wmp: average number of work_mem buffers per connection in percent (default 150)\n";
88155
exit $return;
89156
}
90157

@@ -887,3 +954,18 @@ sub print_advices {
887954
print color("green")."Everything is good".color("reset")."\n";
888955
}
889956
}
957+
958+
sub pgpass_match {
959+
my ($type,$var,$pgp_var)=@_;
960+
$pgp_var=pgpass_unescape($pgp_var);
961+
return 1 if $pgp_var eq '*';
962+
return 1 if $pgp_var eq $var;
963+
return 1 if $type eq 'host' and $pgp_var eq 'localhost' and $var=~m/^\//; # allow sockets if host=localhost
964+
return 0;
965+
}
966+
967+
sub pgpass_unescape {
968+
my ($value)=@_;
969+
$value=~s/\\(.)/$1/g;
970+
return $value;
971+
}

0 commit comments

Comments
 (0)