Test::PostgreSQL - PostgreSQL runner for tests
use DBI; use Test::PostgreSQL; use Test::More; # optionally # (if not already set at shell): # # $ENV{POSTGRES_HOME} = '/path/to/my/pgsql/installation'; my $pgsql = Test::PostgreSQL->new() or plan skip_all => $Test::PostgreSQL::errstr; plan tests => XXX; my $dbh = DBI->connect($pgsql->dsn);
Test::PostgreSQL automatically setups a PostgreSQL instance in a temporary directory, and destroys it when the perl script exits.
Test::PostgreSQL
This module is a fork of Test::postgresql, which was abandoned by its author several years ago.
Create and run a PostgreSQL instance. The instance is terminated when the returned object is being DESTROYed. If required programs (initdb and postmaster) were not found, the function returns undef and sets appropriate message to $Test::PostgreSQL::errstr.
Returns directory under which the PostgreSQL instance is being created. The property can be set as a parameter of the new function, in which case the directory will not be removed at exit.
new
Path to initdb and postmaster which are part of the PostgreSQL distribution. If not set, the programs are automatically searched by looking up $PATH and other prefixed directories. Since postmaster is deprecated in newer PostgreSQL versions postgres is used in preference to postmaster.
initdb
postmaster
postgres
Path to pg_ctl which is part of the PostgreSQL distribution.
pg_ctl
Starting with PostgreSQL version 9.0 <pg_ctl> can be used to start/stop postgres without having to use fork/pipe and will be chosen automatically if "pg_ctl" is not set but the program is found and the version is recent enough.
NOTE: do NOT use this with PostgreSQL versions prior to version 9.0.
Defaults to -U postgres -A trust
-U postgres -A trust
Extra args to be appended to "initdb_args"
Defaults to -h 127.0.0.1 -F
-h 127.0.0.1 -F
Extra args to be appended to "postmaster_args"
Path to psql client which is part of the PostgreSQL distribution.
psql
psql can be used to run SQL scripts against the temporary database created by "new":
my $pgsql = Test::PostgreSQL->new(); my $psql = $pgsql->psql; my $out = `$psql -f /path/to/script.sql 2>&1`; die "Error executing script.sql: $out" unless $? == 0;
Command line arguments necessary for psql to connect to the correct PostgreSQL instance. Defaults to -U postgres -d test -h 127.0.0.1 -p $self-port>
-U postgres -d test -h 127.0.0.1 -p $self-
Extra args to be appended to "psql_args".
Builds and returns dsn by using given parameters (if any). Default username is 'postgres', and dbname is 'test' (an empty database).
Builds and returns a connection URI using the given parameters (if any). See URI::db for details about the format.
Default username is 'postgres', and dbname is 'test' (an empty database).
Returns process id of PostgreSQL (or undef if not running).
Returns TCP port number on which postmaster is accepting connections (or undef if not running).
Starts postmaster.
Stops postmaster.
Setups the PostgreSQL instance.
Whether to only connect via UNIX sockets; if false (the default), connections can occur via localhost. [This changes the "dsn" returned to only give the UNIX socket directory, and avoids any issues with conflicting TCP ports on localhost.]
If your postgres installation is not located in a well known path, or you have many versions installed and want to run your tests against particular one, set this environment variable to the desired path. For example:
export POSTGRES_HOME='/usr/local/pgsql94beta'
This is the same idea and variable name which is used by the installer of DBD::Pg.
Toby Corkindale, Kazuho Oku, Peter Mottram, plus various contributors.
Current version copyright © 2012-2015 Toby Corkindale.
Previous versions copyright (C) 2009 Cybozu Labs, Inc.
This module is free software, released under the Perl Artistic License 2.0. See http://www.perlfoundation.org/artistic_license_2_0 for more information.
To install Test::PostgreSQL, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Test::PostgreSQL
CPAN shell
perl -MCPAN -e shell install Test::PostgreSQL
For more information on module installation, please visit the detailed CPAN module installation guide.