NAME

PGObject::Util::DBAdmin - PostgreSQL Database Management Facilities for PGObject

VERSION

version 1.3.0

SYNOPSIS

This module provides an interface to the basic Postgres db manipulation utilities.

my $db = PGObject::Util::DBAdmin->new(
   connect_data => {
      user     => 'postgres',
      password => 'mypassword',
      host     => 'localhost',
      port     => '5432',
      dbname   => 'mydb'
   }
);

my @dbnames = $db->list_dbs(); # like psql -l

$db->create(); # createdb
$db->run_file(file => 'sql/initial_schema.sql'); # psql -f

my $filename = $db->backup(format => 'c'); # pg_dump -Fc

my $db2 = PGObject::Util::DBAdmin->new($db->export, (dbname => 'otherdb'));

my $db3 = PGObject::Util::DBAdmin->new(
   connect_data => {
      service     => 'zephyr',
      sslmode     => 'require',
      sslkey      => "$HOME/.postgresql/postgresql.key",
      sslcert     => "$HOME/.postgresql/postgresql.crt",
      sslpassword => 'your-sslpassword',
   }
);

PROPERTIES

connect_data

Contains a hash with connection parameters; see the PostgreSQL documentation for supported parameters.

The usual parameters are:

  • user

  • password

  • dbname

  • host

  • port

Please note that the key requiressl is deprecated in favor of sslmode and isn't supported.

username (deprecated)

The username used to authenticate with the PostgreSQL server.

password (deprecated)

The password used to authenticate with the PostgreSQL server.

host (deprecated)

In PostgreSQL, this can refer to the hostname or the absolute path to the directory where the UNIX sockets are set up.

port (deprecated)

Default '5432'

dbname (deprecated)

The database name to create or connect to.

stderr

When applicable, the stderr output captured from any external commands (for example createdb or pg_restore) run during the previous method call. See notes in "CAPTURING".

stdout

When applicable, the stdout output captured from any external commands (for example createdb or pg_restore) run during the previous method call. See notes in "CAPTURING".

GLOBAL VARIABLES

%helper_paths

This hash variable contains as its keys the names of the PostgreSQL helper executables psql, dropdb, pg_dump, etc. The values contain the paths at which the executables to be run are located. The default values are the names of the executables only, allowing them to be looked up in $PATH.

Modification of the values in this variable are the strict realm of applications. Libraries using this library should defer potential required modifications to the applications based upon them.

SUBROUTINES/METHODS

new

Creates a new db admin object for manipulating databases.

BUILDARGS

Compensates for the legacy invocation with the username, password, host, port and dbname parameters.

verify_helpers( [ helpers => [...]], [operations => [...]])

Verifies ability to execute (external) helper applications by method name (through the operations argument) or by external helper name (through the helpers argument). Returns a hash ref with each key being the name of a helper application (see helpers below) with the values being a boolean indicating whether or not the helper can be successfully executed.

Valid values in the array referenced by the operations parameter are create, run_file, backup, backup_globals, restore and drop; the methods this module implements with the help of external helper programs. (Other values may be passed, but unsupported values aren't included in the return value.)

Valid values in the array referenced by the helpers parameter are the names of the PostgreSQL helper programs createdb, dropdb, pg_dump, pg_dumpall, pg_restore and psql. (Other values may be passed, but unsupported values will not be included in the return value.)

When no arguments are passed, all helpers will be tested.

Note: verify_helpers is a class method, meaning it wants to be called as PGObject::Util::DBAdmin-verify_helpers()>.

export

Exports the database parameters as a list so it can be used to create another object.

connect($options)

Connects to the database using DBI and returns a database connection.

Connection options may be specified in the $options hashref.

server_version([$dbname])

Returns a version string (like 9.1.4) for PostgreSQL. Croaks on error.

When a database name is specified, uses that database to connect to, using the credentials specified in the instance.

If no database name is specified, 'template1' is used.

list_dbs([$dbname])

Returns a list of db names.

When a database name is specified, uses that database to connect to, using the credentials specified in the instance.

If no database name is specified, 'template1' is used.

create

Creates a new database.

Croaks on error, returns true on success.

Supported arguments:

copy_of

Creates the new database as a copy of the specified one (using it as a template). Optional parameter. Default is to create a database without a template.

run_file

Run the specified file on the db.

After calling this method, STDOUT and STDERR output from the external utility which runs the file on the database are available as properties $db->stdout and $db->stderr respectively.

Croaks on error. Returns true on success.

Recognized arguments are:

file

Path to file to be run. This is a mandatory argument.

stdout_log

Provided for legacy compatibility. Optional argument. The full path of a file to which STDOUT from the external psql utility will be appended.

errlog

Provided for legacy compatibility. Optional argument. The full path of a file to which STDERR from the external psql utility will be appended.

backup

Creates a database backup file.

After calling this method, STDOUT and STDERR output from the external utility which runs the file on the database are available as properties $db->stdout and $db->stderr respectively.

Unlinks the output file and croaks on error.

Returns the full path of the file containining the backup.

Accepted parameters:

format

The specified format, for example c for custom. Defaults to plain text.

file

Full path of the file to which the backup will be written. If the file does not exist, one will be created with umask 0600. If the file exists, it will be overwritten, but its permissions will not be changed.

If undefined, a file will be created using File::Temp having umask 0600.

tempdir

The directory in which to write the backup file. Optional parameter. Uses File::Temp default if not defined. Ignored if file parameter is given.

compress

Optional parameter. Specifies the compression level to use and is passed to the underlying pg_dump command. Default is no compression.

backup_globals

This creates a file containing a plain text dump of global (inter-db) objects, such as users and tablespaces. It uses pg_dumpall to do this.

Being a plain text file, it can be restored using the run_file method.

Unlinks the output file and croaks on error.

Returns the full path of the file containining the backup.

Accepted parameters:

file

Full path of the file to which the backup will be written. If the file does not exist, one will be created with umask 0600. If the file exists, it will be overwritten, but its permissions will not be changed.

If undefined, a file will be created using File::Temp having umask 0600.

tempdir

The directory in which to write the backup file. Optional parameter. Uses File::Temp default if not defined. Ignored if file parameter is given.

restore

Restores from a saved file. Must pass in the file name as a named argument.

After calling this method, STDOUT and STDERR output from the external restore utility are available as properties $db->stdout and $db->stderr respectively.

Croaks on error. Returns true on success.

Recognized arguments are:

file

Path to file which will be restored to the database. Required.

format

The file format, for example c for custom. Defaults to plain text.

drop

Drops the database. This is not recoverable. Croaks on error, returns true on success.

CAPTURING

This module uses Capture::Tiny to run extenal commands and capture their output, which is made available through the stderr and stdout properties.

This capturing does not work if Perl's standard STDOUT or STDERR filehandles have been localized. In this situation, the localized filehandles are captured, but external system calls are not affected by the localization, so their output is sent to the original filehandles and is not captured.

See the Capture::Tiny documentation for more details.

AUTHOR

Chris Travers, <chris at efficito.com>

BUGS

Please report any bugs or feature requests to bug-pgobject-util-dbadmin at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=PGObject-Util-DBAdmin. 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 PGObject::Util::DBAdmin

You can also look for information at:

ACKNOWLEDGEMENTS

LICENSE AND COPYRIGHT

Copyright 2014-2020 Chris Travers.

This program is distributed under the (Revised) BSD License: http://www.opensource.org/licenses/BSD-3-Clause

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Chris Travers's Organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.