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

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.

Install with:

pip install pytest-postgresql

You will also need to install psycopg2, or one of its alternative packagings such as psycopg2-binary (pre-compiled wheels) or psycopg2cffi (CFFI based, useful on PyPy).

Plugin contains three 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.
  • postgresql_nooproc - a nooprocess fixture, that’s connecting to already running postgresql

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, unixsocketdir='/var/run')
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.

Connecting to already existing postgresql database

Some projects are using already running postgresql servers (ie on docker instances). In order to connect to them, one would be using the postgresql_nooproc fixture.

postgresql_external = factories.postgresql('postgresql_nooproc')

By default the postgresql_nooproc fixture would connect to postgresql instance using 5432 port. Standard configuration options apply to it.

These are the configuration options that are working on all levels with the postgresql_nooproc fixture:


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
Configuration options
PostgreSQL option Fixture factory argument Command line option pytest.ini option Noop process fixture Default
Path to executable executable –postgresql-exec postgresql_exec
host host –postgresql-host postgresql_host yes
port port –postgresql-port postgresql_port yes (5432) random
postgresql user user –postgresql-user postgresql_user yes postgres
password password –postgresql-password postgresql_password yes  
Starting parameters startparams –postgresql-startparams postgresql_startparams
Log filename’s prefix logsprefix –postgresql-logsprefix postgresql_logsprefix
Location for unixsockets unixsocket –postgresql-unixsocketdir postgresql_unixsocketdir
Database name db_name –postgresql-dbname postgresql_dbname
PostgreSQL connection options options –postgresql-options postgresql_options yes  

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

Maintaining database state outside of the fixtures

It is possible and appears it’s used in other libraries for tests, to maintain database state with the use of the pytest-postgresql database managing functionality:

For this import DatabaseJanitor and use its init and drop methods:

from pytest_postgresql.factories import DatabaseJanitor

# variable definition

janitor = DatabaseJanitor(user, host, port, db_name, version)
# your code, or yield
# at this moment you'll have clean database step

or use it as a context manager:

from pytest_postgresql.factories import DatabaseJanitor

# variable definition

with DatabaseJanitor(user, host, port, db_name, version):
    # do something here



  • [feature] Drop support for pyhon 3.5
  • [enhancement] require at least mirakuru 2.3.0 (executor’s stop method parameter’s change)
  • [bug] pass password to DatabaseJanitor in client’s factory


  • [feature] Allow to set password for postgresql. Use it throughout the flow.
  • [bugfix] Default Janitor’s connections to postgres database. When using custom users, postgres attempts to use user’s database and it might not exist.
  • [bugfix] NoopExecutor connects to read version by context manager to properly handle cases where it can’t connect to the server.


  • [bugfix] Fix drop_postgresql_database to actually use DatabaseJanitor.drop instead of an init


  • [feature] ability to properly connect to already existing postgresql server using postgresql_nooproc fixture.


  • [enhancement] Gather helper functions maintaining postgresql database in DatabaseJanitor class.
  • [deprecate] Deprecate init_postgresql_database in favour of DatabaseJanitor.init
  • [deprecate] Deprecate drop_postgresql_database in favour of DatabaseJanitor.drop


  • [feature] Drop support for python 2.7. From now on, only support python 3.5 and up
  • [feature] Ability to configure database name through plugin options
  • [enhancement] Use tmpdir_factory. Drop logsdir parameter
  • [ehnancement] Support only Postgresql 9.0 and up
  • [bugfix] Always start postgresql with LC_ALL, LC_TYPE and LANG set to C.UTF-8. It makes postgresql start in english.


  • [bugfix] Allow creating test databse with hyphens


  • [enhancements] Ability to configure additional options for postgresql process and connection
  • [bugfix] - removed hard dependency on psycopg2, allowing any of its alternative packages, like psycopg2-binary, to be used.
  • [maintenance] Drop support for python 3.4 and use 3.7 instead


  • [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.

Files for pytest-postgresql, version 2.4.0
Filename, size File type Python version Upload date Hashes
Filename, size pytest_postgresql-2.4.0-py3-none-any.whl (31.7 kB) File type Wheel Python version py3 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page