Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a utility shell script to launch PostgreSQL in Docker #165

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,21 @@ the easiest to express:

- Install [tox](http://testrun.org/tox/latest/): `pip install tox`

The pg8000 test suite requires a PostgreSQL server running on the local host.

### Running PostgreSQL server using Docker
If you have Docker installed you may use it to fire up a local PostgreSQL server
to run the tests against, using the provided shell script:
```shell
./postgres_test_server/run-test-server.sh
```
The server will run in the foreground, logging all statements sent to it to the
terminal, until you stop it with Ctrl-C.

### Manual configuration of PostgreSQL server
If you'd like to configure your PostgreSQL server manually instead of going
with Docker, these are the required steps:

- Enable the PostgreSQL hstore extension by running the SQL command:
`create extension hstore;`

Expand All @@ -2077,8 +2092,18 @@ host all all 127.0.0.1/32 trust
- Set password encryption to `scram-sha-256` in `postgresql.conf`:
`password_encryption = 'scram-sha-256'`

- Enable SSL in `postgresql.conf`:
`ssl = on`

- Create a self-signed server certificate for the PostgreSQL server.

- Make sure there is a PostgreSQL user and a database with the same name as
you logged in user.

- Set the password for the postgres user: `ALTER USER postgresql WITH PASSWORD 'pw';`

### Running the tests

- Run `tox` from the `pg8000` directory: `tox`

This will run the tests against the Python version of the virtual environment, on the
Expand Down
36 changes: 36 additions & 0 deletions postgres_test_server/init-scripts/pg8000-init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

cat > /var/lib/postgresql/data/pg_hba.conf <<EOF
host pg8000_md5 all 0.0.0.0/0 md5
host pg8000_gss all 0.0.0.0/0 gss
host pg8000_password all 0.0.0.0/0 password
host pg8000_scram_sha_256 all 0.0.0.0/0 scram-sha-256
host all all 0.0.0.0/0 trust
EOF


# Generate server key and certificate
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 -keyout /var/lib/postgresql/data/server.key -out /var/lib/postgresql/data/server.crt -subj "/CN=localhost"

# Secure the server.key file
chmod 600 /var/lib/postgresql/data/server.key

# Ensure the right owner and permissions for PostgreSQL to use the certificates
chown postgres:postgres /var/lib/postgresql/data/server.crt /var/lib/postgresql/data/server.key

# Enable SSL, set proper password encryption and enable logging of all statements
cat >> /var/lib/postgresql/data/postgresql.conf <<EOF
password_encryption = 'scram-sha-256'

ssl = on
ssl_cert_file = '/var/lib/postgresql/data/server.crt'
ssl_key_file = '/var/lib/postgresql/data/server.key'

log_statement = all
EOF

# Create a user and database with the same name as the "outside" user,
#in order for code in readme.md tests to work

createuser "${OUTSIDE_USER}"
createdb "${OUTSIDE_USER}"
3 changes: 3 additions & 0 deletions postgres_test_server/init-scripts/pg8000-init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CREATE EXTENSION hstore;

CREATE DATABASE pg8000_gss;
16 changes: 16 additions & 0 deletions postgres_test_server/run-test-server.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

# This script will fire up a local postgresql test server using docker

base_path=$(dirname "$0")

docker run \
--interactive \
--tty \
--rm \
--publish 127.0.0.1:5432:5432 \
--env POSTGRES_PASSWORD=pw \
--env OUTSIDE_USER="$USER" \
--mount type=bind,source="$base_path/init-scripts",target=/docker-entrypoint-initdb.d \
--name pg8000-test-server \
postgres:latest "$@"