Postgres testing utility
To installtestgres
run:
At current state it only works with Python 2.*
Node: by default testgres runsinitdb
,pg_ctl
,psql
commands from the$PATH
. To specify custom postgres installation set environment variable$PG_CONFIG
and point it to pg_config executable:export PG_CONFIG=/path/to/pg_config
Here is an example of how you can usetestgres
.
fromtestgresimportget_new_nodetry:node=get_new_node('master')node.init()node.start()stdout=node.safe_psql('postgres','SELECT 1')printstdoutnode.stop()exceptClusterException,e:node.cleanup()
Let's walk through the code. First you create new node:
node=get_new_node('master')
master
here is a node's name, not the database's name. The name matters if you're testing replication. Functionget_new_node()
only creates directory structure in/tmp
for cluster. Next line:
initializes cluster. On low level it runsinitdb
command and adds some basic configuration topostgresql.conf
andpg_hba.conf
files. Functioninit()
accepts optional parameterallows_streaming
which configures cluster for streaming replication (default isFalse
).Now we are ready to start:
After this you are able to run queries over the cluster. There are three functions to do that:
node.psql(database, query)
- runs query viapsql
command and returns tuple (error code, stdout, stderr)node.safe_psql(database, query)
- the same aspsql()
except that it returns onlystdout
. If error occures during the execution then it will throw an exception;node.execute(database, query)
- connects with postgresql server usingpsycopg2
orpg8000
library (depends on which is installed in your system) and returns two-dimensional array with data.
To stop server run:
Please seetestgres/tests
directory for replication configuration example.