Pg::ServiceFile - Basic PostgreSQL connection service file interface
use Pg::ServiceFile; # Uses $ENV{PGSERVICEFILE} or user's `~/.pg_service.conf` file my $pgservice = Pg::ServiceFile->new(); # Use a specific service file - `pg_config --sysconfdir`/pg_service.conf my $pgservice = Pg::ServiceFile->new(file => '/etc/postgresql-common/pg_service.conf'); # Print all the service names say $_ for @{$pgservice->names}; # Get the username for a specific service name say $pgservice->services->{foo}->{user};
Pg::ServiceFile is a partially complete interface to the PostgreSQL connection service file. It's complete in the fact that it reads the $ENV{PGSERVICEFILE} or user service file as standard, but will not automatically retrieve and merge the system-wide service file or check PGSYSCONFDIR.
$ENV{PGSERVICEFILE}
PGSYSCONFDIR
Pg::ServiceFile implements the following attributes.
my $pgservice = Pg::ServiceFile->new(data => <<~'PGSERVICEFILE'); [foo] host=localhost port=5432 user=foo dbname=db_foo password=password PGSERVICEFILE my $pgservice = Pg::ServiceFile->new(file => '~/.pg_service.conf'); say $pgservice->data;
The connection service file data. This is the contents of "file", or the data that has been passed in directly during instantiation.
my $pgservice = Pg::ServiceFile->new(); say $pgservice->file; # ~/.pg_service.conf (if it exists) my $pgservice = Pg::ServiceFile->new(file => '~/myservice.conf'); say $pgservice->file; # ~/myservice.conf
Defaults to $ENV{PGSERVICEFILE} or ~/.pg_service.conf, but can be any valid connection service file.
~/.pg_service.conf
local $ENV{PGSERVICE} = 'foo'; my $pgservice = Pg::ServiceFile->new(); say $pgservice->name; # foo say $pgservice->service->{dbname}; # db_foo
The value of $ENV{PGSERVICE} if it exists, or whatever is set during instantiation. It does not check to see if a corresponding service entry exists in the service "file", but "service" will return the relevant data if it does.
$ENV{PGSERVICE}
my $pgservice = Pg::ServiceFile->new(); say $_ for @{$pgservice->names};
Returns the names of all the connection services from the service "file".
If "name" has been set via $ENV{PGSERVICE} or on instantiation, returns the corresponding connection service.
my $pgservice = Pg::ServiceFile->new(); for my $service ($pgservice->services) { say "$service->{dbname} at $service->{host}"; }
Returns a HASH of all of the connection services from "file".
HASH
Paul Williams <kwakwa@cpan.org>
Copyright 2018- Paul Williams
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
https://www.postgresql.org/docs/current/static/libpq-pgservice.html.
To install Pg::ServiceFile, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Pg::ServiceFile
CPAN shell
perl -MCPAN -e shell install Pg::ServiceFile
For more information on module installation, please visit the detailed CPAN module installation guide.