Postgres

Postgres process management.

Use this module to initialize a postgres data directory and to manage a postgres server.

Environment Variables

POSTGRES_VERSION - if specified, this will be the default version of postgres. The documentation for the Manager class describes how the version of postgres is chosen.

Exceptions

exception servermgr.postgres.DatabaseException(exception, *args, **kwargs)[source]

Database exceptions.

Classes

class servermgr.postgres.Manager(host, port, db_dir, initialize=False, pg_version='', **kwargs)[source]

Postgres manager object.

Selecting the postgres version:

  • Use the version specified by the pg_version parameter.
  • If pg_version is not specified, use the environment variable POSTGRES_VERSION.
  • If the environment variable is not set, use the highest numbered version in the directory /usr/lib/postgresql.
Parameters:
  • host (string) – the interface to listen on.
  • port (int) – the post to listen on.
  • db_dir (string) – the database data directory.
  • initialize (bool) – If this flag is true, the database data directory will be initialized iff the data directory does not already exist.
  • pg_version (string) – Version of postgres to use.
Raises :

DatabaseException if data directory initialization fails.

health()[source]

Check manager health.

Raises :WorkerError if we cannot connect to the database
process

Return the process object.

ready_wait(timeout=10.0, verbose=False)

Wait until the worker subprocess is responding to requests.

Parameters:
  • timeout (float, seconds) – Give the server this many seconds to start. If the timeout expires before the server has started, kill the server and raise WorkerError.
Raises :

WorkerError if the worker is not ready within the time limit.

Raises :

WorkerError if there is no subprocess.

start(wait=True, timeout=10.0)[source]

Launch the postgres process.

Parameters:
  • wait – If true, call self.ready_wait() after starting the subprocess.
  • timeout – When calling ready_wait(), use this timeout value.
Raises :

OSError if postgres fails to launch.

stop(wait=True)

Terminate the worker subprocess.

Parameters:
  • wait (boolean) – If True, wait for the worker to exit.
wait()

Wait for the worker process to exit.

Module Private Functions

servermgr.postgres._check_lockdir_permissions()[source]

Make sure we have permission to create a postgres postfile.

Raises :DatabaseException if we do not have write permission on the lockfile directory.
servermgr.postgres._get_default_postgres_version()[source]

Select the default version of postgres to use.

If the environment variable POSTGRES_VERSION is set, use that. Otherwise, use the largest numbered directory in /usr/lib/postgresql.

Returns:the default version of postgres, or None if we can’t determine the default postgres version.
servermgr.postgres._env_with_postgres_path(pg_version)[source]

Return a new environment with Postgres bin directories append to the end.

servermgr.postgres._initialize_directory(directory, pg_version)[source]

Initialize a postgres data directory.

Parameters:
  • directory – Name of the postgres data directory.
  • pg_version – The version of postgres to use.
Raises :

OSError if subprocess execution fails.

Raises :

DatabaseException on initdb error.

Running postgres.py

servermgr.postgres.main()[source]

You can use this script to start a copy of postgres.

Usage: postgres.py [options]

Options:
-h, --help show this help message and exit
-H HOST, --host=HOST
 listener hostname
-p PORT, --port=PORT
 listener port
-d DATADIR, --datadir=DATADIR
 data directory
-i INITIALIZE, --init=INITIALIZE
 initialize data directory
-v VERSION, --version=VERSION
 postgres version

Table Of Contents

Previous topic

Nginx

Next topic

Django_app

This Page