Test::PostgreSQL(3pm)
π Test::PostgreSQL(3pm) π
okay bestie, so this is a user contributed Perl documentation page, and it's literally the manual for Test::PostgreSQL(3pm) β like, say hi to the page, she's the whole point π
. Buckle up, we're doing package docs but make it cute β¨
πΈ NAME πΈ
Test::PostgreSQL β PostgreSQL runner for tests πβ¨
π SYNOPSIS π
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 = eval { Test::PostgreSQL->new() }
or plan skip_all => $@;
plan tests => XXX;
my $dbh = DBI->connect($pgsql->dsn);
π¦ DESCRIPTION π¦
So Test::PostgreSQL automatically sets up a whole PostgreSQL instance in a temporary directory for you, and then destroys it the second your perl script exits. No cap. It just appears, does its job, and dips π«‘
And for the lore: this module is a fork of Test::postgresql, which its author abandoned several years ago (π rip king, but we move).
β¨ ATTRIBUTES β¨
A Test::PostgreSQL object has all of these attributes, and you can override any of them by passing the matching argument to the constructor π
π dbname
Database name to use in this Test::PostgreSQL instance. Default is test.
π dbowner
Database owner user name. Default is postgres.
π host
Host name or IP address to use for PostgreSQL instance connections. Default is 127.0.0.1.
π base_dir
Base directory under which the PostgreSQL instance is being created. You can pass this as a parameter to the constructor, and if you do, the directory will not be removed at exit π
π base_port
Connection port number to start with. If that port is already being used, we increment the value and try again π
Default: 15432.
π unix_socket
Whether to only connect via UNIX sockets; if false (which is the default), connections can happen via localhost. [This changes the dsn that gets returned so it only gives the UNIX socket directory, and it avoids any issues with conflicting TCP ports on localhost.]
π socket_dir
Unix socket directory to use if unix_socket is true. Default is $base_dir/tmp.
π pg_ctl
Path to the pg_ctl program, which is part of the PostgreSQL distribution.
Starting with PostgreSQL version 9.0, pg_ctl can be used to start/stop postgres without having to use fork/pipe, and it 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. Like, that's not a drill bestie π
By default we will try to find pg_ctl in the PostgreSQL directory.
π initdb
Path to the initdb program, which is part of the PostgreSQL distribution. Default is to try and find it in the PostgreSQL directory.
π initdb_args
Arguments to pass to the initdb program when creating a new PostgreSQL database cluster for a Test::PostgreSQL session.
Defaults to -U postgres -A trust. See dbowner.
π extra_initdb_args
Extra args to be appended to initdb_args. Default is empty.
π pg_config
Configuration to place in $base_dir/data/postgresql.conf. Use this to override PostgreSQL configuration defaults β for example, to speed up PostgreSQL database init and seeding, one might use something like this:
my $pgsql = Test::PostgreSQL->new(
pg_config => q|
# foo baroo mymse throbbozongo
fsync = off
synchronous_commit = off
full_page_writes = off
bgwriter_lru_maxpages = 0
shared_buffers = 512MB
effective_cache_size = 512MB
work_mem = 100MB
|);
π postmaster
Path to postmaster, which is part of the PostgreSQL distribution. If it's 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.
π postmaster_args
Defaults to -h 127.0.0.1 -F.
π extra_postmaster_args
Extra args to be appended to postmaster_args. Default is empty.
π psql
Path to the psql client, which is part of the PostgreSQL distribution.
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;
π psql_args
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.
See also dbowner, dbname, host, base_port.
π extra_psql_args
Extra args to be appended to psql_args.
π run_psql_args
Arguments specific to a run_psql invocation, used mostly to set up and seed the database schema after the PostgreSQL instance is launched and configured.
Default is -1Xqb -v ON_ERROR_STOP=1. And that means:
1: Run all SQL statements in passed scripts as a single transactionX: Skip.psqlrcfilesq: Run quietly, print only notices and errors on stderr (if any)b: Echo SQL statements that cause PostgreSQL exceptions (version 9.5+)-v ON_ERROR_STOP=1: Stop processing SQL statements after the first error
π seed_scripts
Arrayref with the list of SQL scripts to run after the database has been instanced and set up. Default is [].
NOTE thatpsqlolder than 9.6 does not support multiple-cand-fswitches in arguments, soseed_scriptswill be executed one by one. This implies multiple transactions instead of just one; if you need all seed statements to apply within a single transaction, combine them into one seed script. π§
π auto_start
Integer value that controls whether the PostgreSQL server is started and set up after creating the Test::PostgreSQL instance. Possible values:
- Do not start PostgreSQL.
- Start PostgreSQL but do not run
setup. - Start PostgreSQL and run
setup.
Default is 2. π
π METHODS π
π new
Create and run a PostgreSQL instance. The instance is terminated when the returned object is being DESTROYed. If the required programs (initdb and postmaster) were not found, the function returns undef and sets an appropriate message to $Test::PostgreSQL::errstr.
π dsn
Builds and returns dsn by using given parameters (if any). Default username is postgres, and dbname is test (an empty database).
π uri
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).
π pid
Returns the process id of PostgreSQL (or undef if it's not running).
π port
Returns the TCP port number on which postmaster is accepting connections (or undef if it's not running).
π start
Starts postmaster.
π stop
Stops postmaster.
π setup
Sets up the PostgreSQL instance. Note that this method should be invoked before start.
π run_psql
Execute the psql program with the given list of arguments. Usually this would be something like:
$pgsql->run_psql('-c', q|'INSERT INTO foo (bar) VALUES (42)'|);
Or:
$pgsql->run_psql('-f', '/path/to/script.sql');
Note that when using literal SQL statements with the -c parameter you will need to escape them manually like shown above. run_psql will not quote them for you, bestie, that's on you π
The actual command line to execute psql will be concatenated from psql_args, extra_psql_args, and run_psql_args.
NOTE thatpsqlolder than 9.6 does not support multiple-cand/or-fswitches in arguments.
π run_psql_scripts
Given a list of script file paths, invoke run_psql once with -f 'script' for every path in PostgreSQL 9.6+, or once per -f 'script' for older PostgreSQL versions.
π§ ENVIRONMENT π§
π¦ POSTGRES_HOME
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 one particular version, set this environment variable to the desired path. For example:
export POSTGRES_HOME='/usr/local/pgsql94beta'
This is the same idea and the same variable name that's used by the installer of DBD::Pg.
π AUTHOR π
Toby Corkindale, Kazuho Oku, Peter Mottram, Alex Tokarev, plus various contributors. Legends, all of them π«Ά
π COPYRIGHT π
Current version copyright X 2012-2015 Toby Corkindale.
Previous versions copyright (C) 2009 Cybozu Labs, Inc.
β LICENSE β
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.
and that's the whole page, bestie, no notes β¨ she's perl v5.36.0 coded, dated 2023-01-18, and officially filed under Test::PostgreSQL(3pm) β mwah, slay, go write some tests ππ
π¦ where this page comes from
- ubuntu noble: libtest-postgresql-perl 1.29-1build1 Β· Artistic-2.0
- ubuntu resolute: libtest-postgresql-perl 1.29-1build1 Β· Artistic-2.0
- ubuntu stonking: libtest-postgresql-perl 1.29-1build1 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms