├── Dockerfile ├── Readme.md └── config.sh /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM postgres:9.4 2 | 3 | MAINTAINER labianchin 4 | 5 | ENV PGDATA /var/lib/postgresql/data 6 | COPY config.sh /docker-entrypoint-initdb.d/ 7 | 8 | ENTRYPOINT ["/docker-entrypoint.sh"] 9 | 10 | EXPOSE 5432 11 | CMD ["postgres"] -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | 2 | # Docker Postgres For Testing 3 | 4 | [![Docker Hub](https://img.shields.io/badge/docker-ready-blue.svg)](https://registry.hub.docker.com/u/labianchin/docker-postgres-for-testing/) 5 | [![Docker Stars](https://img.shields.io/docker/stars/labianchin/docker-postgres-for-testing.svg)](https://registry.hub.docker.com/u/labianchin/docker-postgres-for-testing/) 6 | [![Docker Pulls](https://img.shields.io/docker/pulls/labianchin/docker-postgres-for-testing.svg)](https://registry.hub.docker.com/u/labianchin/docker-postgres-for-testing/) 7 | [![Image Size](https://img.shields.io/imagelayers/image-size/labianchin/docker-postgres-for-testing/latest.svg)](https://imagelayers.io/?images=labianchin/docker-postgres-for-testing:latest) 8 | [![Image Layers](https://img.shields.io/imagelayers/layers/labianchin/docker-postgres-for-testing/latest.svg)](https://imagelayers.io/?images=labianchin/docker-postgres-for-testing:latest) 9 | 10 | This a docker image based in the [official Postgres 9.4 docker image](https://registry.hub.docker.com/_/postgres/) tweaked for database testing. 11 | 12 | It basically configure things like turning off write ahead log (`fsync=off`) to make it faster. Notice that this can make the database more likely to be in an inconsistent state, if the case of a server crash. This is not a problem for database testing as we are more concerned with fast feedback and not about loosing data. 13 | 14 | This is an alternative to [H2](http://www.h2database.com/html/main.html), [in memory SQLite](https://www.sqlite.org/inmemorydb.html) and [HyperSQL](http://hsqldb.org/). You should consider this as it runs a real PostgreSQL server, that would be very close on what you have in production. 15 | 16 | Check the file `config.sh` for all the configurations. 17 | 18 | References: 19 | 20 | - https://stackoverflow.com/questions/9407442/optimise-postgresql-for-fast-testing 21 | - http://michael.robellard.com/2015/07/dont-test-with-sqllite-when-you-use.html 22 | 23 | ## Tips for writing tests 24 | 25 | - Do all schema setup (DDL) once before running the tests 26 | - Run the schema setup (DDL), as the DB migration that would run in production 27 | - Avoid DDL in each test, as that tent to be very slow 28 | - Before each test, truncate the tables and put some seed data (DML), that should be quick 29 | - Remember fasts tests are important, slow tests make you avoid refactoring code! 30 | 31 | 32 | ## TODO 33 | 34 | - Tweak `shared_buffers` 35 | - Tweak `work_mem` 36 | -------------------------------------------------------------------------------- /config.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit 4 | set -o nounset 5 | 6 | echo "Configuring psql with improved performance..." 7 | 8 | sed -ri "s/^#*(fsync\s*=\s*)\S+/\1 off/" "$PGDATA"/postgresql.conf 9 | sed -ri "s/^#*(full_page_writes\s*=\s*)\S+/\1 off/" "$PGDATA"/postgresql.conf 10 | sed -ri "s/^#*(random_page_cost\s*=\s*)\S+/\1 2.0/" "$PGDATA"/postgresql.conf 11 | sed -ri "s/^#*(checkpoint_segments\s*=\s*)\S+/\1 64/" "$PGDATA"/postgresql.conf 12 | sed -ri "s/^#*(checkpoint_completion_target\s*=\s*)\S+/\1 0.9/" "$PGDATA"/postgresql.conf 13 | 14 | grep fsync "$PGDATA"/postgresql.conf 15 | 16 | --------------------------------------------------------------------------------