DBIx::Conn::Pg - Shortcut to connect to PostgreSQL database
This document describes version 0.001 of DBIx::Conn::Pg (from Perl distribution DBIx-Conn-Pg), released on 2018-07-08.
In the command-line, instead of saying:
% perl -MDBI -E'my $dbh = DBI->connect("dbi:Pg:", "", ""); $dbh->selectrow_array("query"); ...' ;# connect to database with the same name as current user % perl -MDBI -E'my $dbh = DBI->connect("dbi:Pg:dbname=mydb", "someuser", "somepass"); $dbh->selectrow_array("query"); ...'
you can just say:
% perl -MDBI::Conn::Pg -E'$dbh->selectrow_array("query"); ...' ;# connect to database with the same name as current user % perl -MDBI::Conn::Pg=mydb,someuser,somedb -E'$dbh->selectrow_array("query"); ...'
To connect with some other DBD::Pg parameters:
% perl -MDBIx::Conn::Pg='dbname=mydb;host=192.168.1.10' -E'$dbh->selectrow_array("query"); ...'
To change the exported database variable name from the default '$dbh'
% perl -MDBIx::Conn::Pg=mydb,'$handle' -E'$handle->selectrow_array("query"); ...'
To supply username and password:
% perl -MDBIx::Conn::Pg=mydb,myuser,mysecret -E'$handle->selectrow_array("query"); ...'
To supply connection attributes:
% perl -MDBIx::Conn::Pg=mydb,myuser,mysecret,AutoCommit,0 -E'$handle->selectrow_array("query"); ...'
This module offers some saving in typing when connecting to a PostgreSQL database using DBI, and is particularly handy in one-liners. It automatically connect() and exports the database handle $dbh for you.
connect()
$dbh
You often only have to specify the database name in the import argument:
-MDBIx::Conn::Pg=mydb
This will result in the following DSN:
DBI:Pg:dbname=mydb
If you need to specify other parameters in the DSN, e.g. when the host is not localhost, or the port is not the default port, you can specify that in the first import argument too (note the quoting because the shell will interpret ; as command separator). When the first import argument contains =, the module knows that you want to specify DSN parameters:
host
localhost
port
;
=
-MDBIx::Conn::Pg='dbname=mydb;host=192.168.1.10;port=23306'
this will result in the following DSN:
'DBI:Pg:dbname=mydb;host=192.168.1.10;port=23306
If you want to use another variable name other than the default $dbh for the database handle, you can specify this in the second import argument (note the quoting because otherwise the shell will substitute with shell variable):
-MDBIx::Conn::Pg=mydb,'$handle'
If you want to supply username and password anyway, you can do that via the third and fourth import arguments (or the second and third import arguments, as long as the username does not begin with $):
$
-MDBIx::Conn::Pg=mydb,'$handle',myuser,mysecret -MDBIx::Conn::Pg=mydb,myuser,mysecret
But note that it's more recommended to specify password using the .pgpass mechanism.
.pgpass
Lastly, if you want to specify connection attributes, you can do that via the fifth arguments and the rest (or the fourth and the rest, if you don't specify custom handle name):
-DBIx::Conn::Pg=mydb,,,AutoCommit,0
Please visit the project's homepage at https://metacpan.org/release/DBIx-Conn-Pg.
Source repository is at https://github.com/perlancar/perl-DBIx-Conn-Pg.
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Conn-Pg
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
DBIx::Conn::MySQL
DBIx::Conn::SQLite
perlancar <perlancar@cpan.org>
This software is copyright (c) 2018 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install DBIx::Conn::Pg, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Conn::Pg
CPAN shell
perl -MCPAN -e shell install DBIx::Conn::Pg
For more information on module installation, please visit the detailed CPAN module installation guide.