Skip to main content

Postgresql fixtures and fixture factories for Pytest.

Project description


Latest PyPI version Wheel Status Supported Python Versions License

Package status

Tests Coverage Status Requirements Status

What is this?

This is a pytest plugin, that enables you to test your code that relies on a running PostgreSQL Database. It allows you to specify fixtures for PostgreSQL process and client.

How to use


Tested on PostgreSQL versions > 9.x. See tests for more details.

Plugin contains two fixtures

  • postgresql - it’s a client fixture that has functional scope. After each test it ends all leftover connections, and drops test database from PostgreSQL ensuring repeatability.
  • postgresql_proc - session scoped fixture, that starts PostgreSQL instance at it’s first use and stops at the end of the tests.

Simply include one of these fixtures into your tests fixture list.

You can also create additional postgresql client and process fixtures if you’d need to:

from pytest_postgresql import factories

postgresql_my_proc = factories.postgresql_proc(
    port=None, logsdir='/tmp')
postgresql_my = factories.postgresql('postgresql_my_proc')


Each PostgreSQL process fixture can be configured in a different way than the others through the fixture factory arguments.


You can define your settings in three ways, it’s fixture factory argument, command line option and pytest.ini configuration option. You can pick which you prefer, but remember that these settings are handled in the following order:

  • Fixture factory argument
  • Command line option
  • Configuration option in your pytest.ini file
PostgreSQL option Fixture factory argument Command line option pytest.ini option Default
Path to executable executable –postgresql-exec postgresql_exec /usr/lib/postgresql/9.1/bin/pg_ctl
host host –postgresql-host postgresql_host
port port –postgresql-port postgresql_port random
postgresql user user –postgresql-user postgresql_user postgres
Starting parameters startparams –postgresql-startparams postgresql_startparams -w
Log directory location logsdir –postgresql-logsdir postgresql_logsdir $TMPDIR
Log filename’s prefix logsprefix –postgresql-logsprefix postgresql_logsprefix  
Location for unixsockets unixsocket –postgresql-unixsocketdir postgresql_unixsocketdir $TMPDIR

Example usage:

  • pass it as an argument in your own fixture

    postgresql_proc = factories.postgresql_proc(
  • use --postgresql-port command line option when you run your tests

    py.test tests --postgresql-port=8888
  • specify your port as postgresql_port in your pytest.ini file.

    To do so, put a line like the following under the [pytest] section of your pytest.ini:

    postgresql_port = 8888



  • [bugfix] properly detect if executor running and clean after executor is being stopped


    Previously if a test failed, there was a possibility of the executor being removed when python was closing, causing it to print ignored errors on already unloaded modules.


  • [enhancement] use executor’s context manager to start/stop postrgesql server in a fixture


  • [bugfix] version regexp to correctly catch postgresql 10


  • [enhancement] explicitly turn off logging_collector


  • [feature] pypy compatibility


  • [bugfix] - disallow connection to database before it gets dropped.


    Otherwise it caused random test subprocess to connect again and this the drop was unsucessfull which resulted in many more test failes on setup.

  • [cleanup] - removed dependency


  • [bugfix] - Fixing the default pg_ctl path creation


  • [feature] - migrate usage of getfuncargvalue to getfixturevalue. require at least pytest 3.0.0


  • create command line and pytest.ini configuration options for postgresql starting parameters
  • create command line and pytest.ini configuration options for postgresql username
  • make the port random by default
  • create command line and pytest.ini configuration options for executable
  • create command line and pytest.ini configuration options for host
  • create command line and pytest.ini configuration options for port
  • Extracted code from pytest-dbfixtures

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
pytest_postgresql-1.3.4-py2.py3-none-any.whl (15.8 kB) Copy SHA256 hash SHA256 Wheel py2.py3
pytest-postgresql-1.3.4.tar.gz (17.9 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page