All Projects → postgrespro → Testgres

postgrespro / Testgres

Licence: other
Testing framework for PostgreSQL and its extensions

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Testgres

Tunnel
PG数据同步工具(Java实现)
Stars: ✭ 122 (+43.53%)
Mutual labels:  postgresql, postgres, replication
Postgresql cluster
PostgreSQL High-Availability Cluster (based on "Patroni" and "DCS(etcd)"). Automating deployment with Ansible.
Stars: ✭ 294 (+245.88%)
Mutual labels:  postgresql, postgres, replication
Amazonriver
amazonriver 是一个将postgresql的实时数据同步到es或kafka的服务
Stars: ✭ 198 (+132.94%)
Mutual labels:  postgresql, postgres, replication
Wal E
Continuous Archiving for Postgres
Stars: ✭ 3,313 (+3797.65%)
Mutual labels:  postgresql, postgres, replication
Pg chameleon
MySQL to PostgreSQL replica system
Stars: ✭ 274 (+222.35%)
Mutual labels:  postgresql, postgres, replication
Repmgr
A lightweight replication manager for PostgreSQL (Postgres) - latest version 5.2.1 (2020-12-07)
Stars: ✭ 1,207 (+1320%)
Mutual labels:  postgresql, postgres, replication
Pg variables
Session wide variables for PostgreSQL
Stars: ✭ 44 (-48.24%)
Mutual labels:  postgresql, postgres
Barman
Barman - Backup and Recovery Manager for PostgreSQL
Stars: ✭ 1,044 (+1128.24%)
Mutual labels:  postgresql, postgres
Postgresclientkit
A PostgreSQL client library for Swift. Does not require libpq.
Stars: ✭ 49 (-42.35%)
Mutual labels:  postgresql, postgres
Postgres
Async Postgres client for PHP based on Amp.
Stars: ✭ 56 (-34.12%)
Mutual labels:  postgresql, postgres
Toro
Multithreaded message processing on Postgres
Stars: ✭ 39 (-54.12%)
Mutual labels:  postgresql, postgres
Fullstack Apollo Express Postgresql Boilerplate
💥 A sophisticated GraphQL with Apollo, Express and PostgreSQL boilerplate project.
Stars: ✭ 1,079 (+1169.41%)
Mutual labels:  postgresql, postgres
Django Tsvector Field
Django field for tsvector (PostgreSQL full text search vector) with managed stored procedure and triggers.
Stars: ✭ 56 (-34.12%)
Mutual labels:  postgresql, postgres
Sql
MySQL & PostgreSQL pipe
Stars: ✭ 81 (-4.71%)
Mutual labels:  postgresql, postgres
Pgtools
Gui application to monitor postgres database events in real time
Stars: ✭ 42 (-50.59%)
Mutual labels:  postgresql, postgres
Scala Db Codegen
Scala code/boilerplate generator from a db schema
Stars: ✭ 49 (-42.35%)
Mutual labels:  postgresql, postgres
Ar Uuid
Override migration methods to support UUID columns without having to be explicit about it.
Stars: ✭ 41 (-51.76%)
Mutual labels:  postgresql, postgres
Ptgo
PostgreSQL Triggers in Go
Stars: ✭ 55 (-35.29%)
Mutual labels:  postgresql, postgres
Osmium
Online collaborative fitting tool.
Stars: ✭ 74 (-12.94%)
Mutual labels:  postgresql, postgres
Rpg Boilerplate
Relay (React), Postgres, and Graphile (GraphQL): A Modern Frontend and API Boilerplate
Stars: ✭ 62 (-27.06%)
Mutual labels:  postgresql, postgres

Build Status codecov PyPI version

Documentation

testgres

PostgreSQL testing utility. Both Python 2.7 and 3.3+ are supported.

Installation

To install testgres, run:

pip install testgres

We encourage you to use virtualenv for your testing environment.

Usage

Environment

Note: by default testgres runs initdb, pg_ctl, psql provided by PATH.

There are several ways to specify a custom postgres installation:

  • export PG_CONFIG environment variable pointing to the pg_config executable;
  • export PG_BIN environment variable pointing to the directory with executable files.

Example:

export PG_BIN=$HOME/pg_10/bin
python my_tests.py

Examples

Here is an example of what you can do with testgres:

# create a node with random name, port, etc
with testgres.get_new_node() as node:

    # run inidb
    node.init()

    # start PostgreSQL
    node.start()

    # execute a query in a default DB
    print(node.execute('select 1'))

# ... node stops and its files are about to be removed

There are four API methods for runnig queries:

Command Description
node.psql(query, ...) Runs query via psql command and returns tuple (error code, stdout, stderr).
node.safe_psql(query, ...) Same as psql() except that it returns only stdout. If an error occures during the execution, an exception will be thrown.
node.execute(query, ...) Connects to PostgreSQL using psycopg2 or pg8000 (depends on which one is installed in your system) and returns two-dimensional array with data.
node.connect(dbname, ...) Returns connection wrapper (NodeConnection) capable of running several queries within a single transaction.

The last one is the most powerful: you can use begin(isolation_level), commit() and rollback():

with node.connect() as con:
    con.begin('serializable')
    print(con.execute('select %s', 1))
    con.rollback()

Logging

By default, cleanup() removes all temporary files (DB files, logs etc) that were created by testgres' API methods. If you'd like to keep logs, execute configure_testgres(node_cleanup_full=False) before running any tests.

Note: context managers (aka with) call stop() and cleanup() automatically.

testgres supports python logging, which means that you can aggregate logs from several nodes into one file:

import logging

# write everything to /tmp/testgres.log
logging.basicConfig(filename='/tmp/testgres.log')

# enable logging, and create two different nodes
testgres.configure_testgres(use_python_logging=True)
node1 = testgres.get_new_node().init().start()
node2 = testgres.get_new_node().init().start()

# execute a few queries
node1.execute('select 1')
node2.execute('select 2')

# disable logging
testgres.configure_testgres(use_python_logging=False)

Look at tests/test_simple.py file for a complete example of the logging configuration.

Backup & replication

It's quite easy to create a backup and start a new replica:

with testgres.get_new_node('master') as master:
    master.init().start()

    # create a backup
    with master.backup() as backup:

        # create and start a new replica
        replica = backup.spawn_replica('replica').start()

        # catch up with master node
        replica.catchup()

        # execute a dummy query
        print(replica.execute('postgres', 'select 1'))

Benchmarks

testgres is also capable of running benchmarks using pgbench:

with testgres.get_new_node('master') as master:
    # start a new node
    master.init().start()

    # initialize default DB and run bench for 10 seconds
    res = master.pgbench_init(scale=2).pgbench_run(time=10)
    print(res)

Custom configuration

It's often useful to extend default configuration provided by testgres.

testgres has default_conf() function that helps control some basic options. The append_conf() function can be used to add custom lines to configuration lines:

ext_conf = "shared_preload_libraries = 'postgres_fdw'"

# initialize a new node
with testgres.get_new_node().init() as master:

    # ... do something ...
	
    # reset main config file
    master.default_conf(fsync=True,
                        allow_streaming=True)

    # add a new config line
    master.append_conf('postgresql.conf', ext_conf)

Note that default_conf() is called by init() function; both of them overwrite the configuration file, which means that they should be called before append_conf().

Authors

Ildar Musin
Dmitry Ivanov
Ildus Kurbangaliev
Yury Zhuravlev

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].