You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+46-4Lines changed: 46 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -4,20 +4,62 @@ Postgres testing utility
4
4
5
5
## Installation
6
6
7
-
`TODO`
7
+
To install `testgres` run:
8
+
9
+
```
10
+
pip install testgres
11
+
```
12
+
13
+
At current state it only works with Python 2.*
8
14
9
15
## Usage
10
16
11
-
```
17
+
18
+
> Node: by default testgres runs `initdb`, `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`
19
+
20
+
Here is an example of how you can use `testgres`
21
+
22
+
```python
12
23
from testgres import get_new_node
13
24
14
25
try:
15
-
node = get_new_node('test')
26
+
node = get_new_node('master')
16
27
node.init()
17
28
node.start()
18
-
stdout = node.psql('postgres', 'SELECT 1')
29
+
stdout = node.safe_psql('postgres', 'SELECT 1')
19
30
print stdout
20
31
node.stop()
21
32
except ClusterException, e:
22
33
node.cleanup()
23
34
```
35
+
36
+
Let's walk through the code. First you create new node:
37
+
38
+
```python
39
+
node = get_new_node('master')
40
+
```
41
+
42
+
`master` here is a node's name, not the database's name. The name matters if you're testing replication. Function get_new_node() only creates directory structure in `/tmp` for cluster. Next line:
43
+
44
+
```python
45
+
node.init()
46
+
```
47
+
48
+
initializes claster. On low level it runs `initdb` command and adds some basic configuration to `postgresql.conf` and `pg_hba.conf` files. Function `init()` accepts optional parameter `allows_streaming` which configures cluster for streaming replication (default is `False`).
49
+
Now we are ready to start:
50
+
51
+
```python
52
+
node.start()
53
+
```
54
+
55
+
After this you are able to run queries over the cluster. There is three functions to do that:
56
+
57
+
* node.psql(database, query) - runs query via `psql` command and returns tuple (error code, stdout, stderr)
58
+
* node.safe_psql(database, query) - the same as `psql()` except that it returns only `stdout`. If error occures during the execution then it will throw an exception;
59
+
* node.execute(database, query) - connects with postgresql server using `psycopg2` or `pg8000` library (depends on which is installed in your system) and returns two-dimensional array with data.
0 commit comments