NAME
Test::PostgreSQL::v2 - A modern, isolated PostgreSQL executable runner for tests
VERSION
Version 2.00
SYNOPSIS
use DBI;
use Test::PostgreSQL::v2;
# Start a temporary PostgreSQL instance
my $pg = Test::PostgreSQL::v2->new()
or die $Test::PostgreSQL::v2::errstr;
# Connect directly with DBI
my $dbh = DBI->connect($pg->dsn, $pg->user, '')
or die $DBI::errstr;
$dbh->do("CREATE TABLE foo (id SERIAL PRIMARY KEY, name TEXT)");
$dbh->disconnect;
# The instance is automatically stopped and cleaned up when $pg goes out of scope
Usage with Test::DBIx::Class
The recommended way to use this module with Test::DBIx::Class is via the bundled Testpostgresqlv2 trait, which handles all lifecycle and compile-time scoping automatically:
use Test::More;
use Test::DBIx::Class {
schema_class => 'MyApp::Schema',
traits => ['Testpostgresqlv2'],
deploy_db => 1,
}, qw/:resultsets/;
ok ResultSet('User')->create({ name => 'John' }), 'Created user';
done_testing;
If you need manual control over the connection instead, you must start the instance at compile time to satisfy use Test::DBIx::Class's import phase:
use Test::More;
our $pg;
BEGIN {
require Test::PostgreSQL::v2;
$pg = Test::PostgreSQL::v2->new()
or die $Test::PostgreSQL::v2::errstr;
}
use Test::DBIx::Class {
schema_class => 'MyApp::Schema',
connect_info => [ $pg->dsn, $pg->user, '' ],
deploy_db => 1,
}, qw/:resultsets/;
done_testing;
DESCRIPTION
Test::PostgreSQL::v2 is a "clean sheet" rewrite designed to replace the aging Test::PostgreSQL. It addresses modern Linux environment constraints (like Ubuntu 24.04+) by isolating sockets in temporary directories and correctly discovering modern Postgres binaries.
CONSTRUCTOR
new(%args)
The new method initialises and starts a temporary PostgreSQL instance. It performs the following steps:
- 1. Binary Discovery: Searches for
initdbandpostgres(orpostmaster). It respects thePOSTGRES_HOMEenvironment variable if set, falling back to the systemPATH. - 2. Workspace Isolation: Creates a unique
tempdirfor the data cluster and unix sockets (via the-kflag). This avoids permission conflicts with system-wide PostgreSQL socket directories like/var/run/postgresql. - 3. Automatic Port Selection: If no port is provided, it dynamically searches for an available port on the specified host.
- 4. Cluster Initialisation: Runs
initdbwithtrustauthentication and--nosyncfor maximum test performance. - 5. Daemonisation & Readiness: Forks the server process and polls the host/port until the instance is ready to accept connections. If the server fails to start, it slurps the
pg.loginto acroakmessage for immediate debugging.
Arguments:
host(Default: 127.0.0.1)The bind address for the server.
port(Default: Auto-selected)The TCP port to listen on.
user(Default: 'postgres')The superuser name to be created during
initdb.
Returns: A Test::PostgreSQL::v2 object.
dsn()
Returns a string formatted for DBI connection. Example: dbi:Pg:dbname=postgres;host=127.0.0.1;port=54321
user()
Returns the username configured for the database (defaults to 'postgres').
host()
Returns the host/IP the database is listening on.
port()
Returns the port the database is listening on.
pid()
Returns the process ID of the backgrounded PostgreSQL server.
CAVEATS
Process Management
The PostgreSQL server process is tied to the lifecycle of the Perl object. When the object goes out of scope, the DESTROY method attempts a SIGTERM, waits up to 5 seconds for a clean shutdown, and falls back to SIGKILL if the process remains stubborn.
If the Perl interpreter crashes via SIGKILL or an abrupt BAIL_OUT, the background PostgreSQL process may remain running (orphaned). Always ensure your test runner handles cleanup or use a process supervisor in CI environments.
Data Persistence
This module uses File::Temp with CLEANUP => 1. All database data, configuration files, and logs are permanently deleted when the object is destroyed. This is intentional for unit testing but makes the module unsuitable for persistent local development databases.
PACKAGE VARIABLES
$errstr
Set to a human-readable error message when new() fails and returns undef. Cleared to an empty string at the start of each new() call.
my $pg = Test::PostgreSQL::v2->new()
or die $Test::PostgreSQL::v2::errstr;
ENVIRONMENT VARIABLES
POSTGRES_HOME
If set, the module will look for initdb and postgres binaries in $POSTGRES_HOME/bin. This is useful for testing against specific versions of PostgreSQL installed in non-standard locations (e.g., /usr/lib/postgresql/16/).
SEE ALSO
-
The original inspiration for this module. While it served the community for years, it may struggle with modern Linux socket permissions and binary naming conventions addressed here in
Test::PostgreSQL::v2. -
The gold standard for ORM-based testing.
Test::PostgreSQL::v2is designed to be a reliable backend engine forTest::DBIx::Classconfigurations. -
Essential for handling PostgreSQL-specific date and time formats. Verified compatible with this module's data handling in integration tests.
-
The underlying database interface and driver required to communicate with the instances spawned by this module.
AUTHOR
Mohammad Sajid Anwar, <mohammad.anwar at yahoo.com>
REPOSITORY
https://github.com/manwar/Test-PostgreSQL-v2
BUGS
Please report any bugs or feature requests through the web interface at https://github.com/manwar/Test-PostgreSQL-v2/issues. I will be notified and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc Test::PostgreSQL::v2
You can also look for information at:
BUG Report
CPAN Ratings
Search MetaCPAN
LICENSE AND COPYRIGHT
Copyright (C) 2026 Mohammad Sajid Anwar.
This program is free software; you can redistribute it and / or modify it under the terms of the the Artistic License (2.0). You may obtain a copy of the full license at: http://www.perlfoundation.org/artistic_license_2_0 Any use, modification, and distribution of the Standard or Modified Versions is governed by this Artistic License.By using, modifying or distributing the Package, you accept this license. Do not use, modify, or distribute the Package, if you do not accept this license. If your Modified Version has been derived from a Modified Version made by someone other than you,you are nevertheless required to ensure that your Modified Version complies with the requirements of this license. This license does not grant you the right to use any trademark, service mark, tradename, or logo of the Copyright Holder. This license includes the non-exclusive, worldwide, free-of-charge patent license to make, have made, use, offer to sell, sell, import and otherwise transfer the Package with respect to any patent claims licensable by the Copyright Holder that are necessarily infringed by the Package. If you institute patent litigation (including a cross-claim or counterclaim) against any party alleging that the Package constitutes direct or contributory patent infringement,then this Artistic License to you shall terminate on the date that such litigation is filed. Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.