|
| 1 | +PostgreSQL Docker image |
| 2 | +======================= |
| 3 | + |
| 4 | +This repository contains Dockerfiles for PostgreSQL images for general usage and OpenShift. |
| 5 | +Users can choose between RHEL and CentOS based images. |
| 6 | + |
| 7 | + |
| 8 | +Environment variables and volumes |
| 9 | +---------------------------------- |
| 10 | + |
| 11 | +The image recognizes the following environment variables that you can set during |
| 12 | +initialization by passing `-e VAR=VALUE` to the Docker run command. |
| 13 | + |
| 14 | +| Variable name | Description | |
| 15 | +| :--------------------------- | ---------------------------------------------- | |
| 16 | +| `POSTGRESQL_USER` | User name for PostgreSQL account to be created | |
| 17 | +| `POSTGRESQL_PASSWORD` | Password for the user account | |
| 18 | +| `POSTGRESQL_DATABASE` | Database name | |
| 19 | +| `POSTGRESQL_ADMIN_PASSWORD` | Password for the `postgres` admin account (optional) | |
| 20 | + |
| 21 | +The following environment variables influence the PostgreSQL configuration file. They are all optional. |
| 22 | + |
| 23 | +| Variable name | Description | Default |
| 24 | +| :---------------------------- | ----------------------------------------------------------------------- | ------------------------------- |
| 25 | +| `POSTGRESQL_MAX_CONNECTIONS` | The maximum number of client connections allowed | 100 |
| 26 | +| `POSTGRESQL_MAX_PREPARED_TRANSACTIONS` | Sets the maximum number of transactions that can be in the "prepared" state. If you are using prepared transactions, you will probably want this to be at least as large as max_connections | 0 |
| 27 | +| `POSTGRESQL_SHARED_BUFFERS` | Sets how much memory is dedicated to PostgreSQL to use for caching data | 32M |
| 28 | +| `POSTGRESQL_EFFECTIVE_CACHE_SIZE` | Set to an estimate of how much memory is available for disk caching by the operating system and within the database itself | 128M |
| 29 | + |
| 30 | +You can also set the following mount points by passing the `-v /host:/container` flag to Docker. |
| 31 | + |
| 32 | +| Volume mount point | Description | |
| 33 | +| :----------------------- | ------------------------------------- | |
| 34 | +| `/var/lib/pgsql/data` | PostgreSQL database cluster directory | |
| 35 | + |
| 36 | +**Notice: When mouting a directory from the host into the container, ensure that the mounted |
| 37 | +directory has the appropriate permissions and that the owner and group of the directory |
| 38 | +matches the user UID or name which is running inside the container.** |
| 39 | + |
| 40 | +Usage |
| 41 | +---------------------- |
| 42 | + |
| 43 | +For this, we will assume that you are using the `rhscl/postgresql-96-rhel7` image. |
| 44 | +If you want to set only the mandatory environment variables and not store the database |
| 45 | +in a host directory, execute the following command: |
| 46 | + |
| 47 | +``` |
| 48 | +$ docker run -d --name postgresql_database -e POSTGRESQL_USER=user -e POSTGRESQL_PASSWORD=pass -e POSTGRESQL_DATABASE=db -p 5432:5432 rhscl/postgresql-96-rhel7 |
| 49 | +``` |
| 50 | + |
| 51 | +This will create a container named `postgresql_database` running PostgreSQL with |
| 52 | +database `db` and user with credentials `user:pass`. Port 5432 will be exposed |
| 53 | +and mapped to the host. If you want your database to be persistent across container |
| 54 | +executions, also add a `-v /host/db/path:/var/lib/pgsql/data` argument. This will be |
| 55 | +the PostgreSQL database cluster directory. |
| 56 | + |
| 57 | +If the database cluster directory is not initialized, the entrypoint script will |
| 58 | +first run [`initdb`](https://www.postgresql.org/docs/9.6/static/app-initdb.html) |
| 59 | +and setup necessary database users and passwords. After the database is initialized, |
| 60 | +or if it was already present, [`postgres`](https://www.postgresql.org/docs/9.6/static/app-postgres.html) |
| 61 | +is executed and will run as PID 1. You can stop the detached container by running |
| 62 | +`docker stop postgresql_database`. |
| 63 | + |
| 64 | +PostgreSQL auto-tuning |
| 65 | +-------------------- |
| 66 | + |
| 67 | +When the PostgreSQL image is run with the `--memory` parameter set and if there |
| 68 | +are no values provided for `POSTGRESQL_SHARED_BUFFERS` and |
| 69 | +`POSTGRESQL_EFFECTIVE_CACHE_SIZE` those values are automatically calculated |
| 70 | +based on the value provided in the `--memory` parameter. |
| 71 | + |
| 72 | +The values are calculated based on the |
| 73 | +[upstream](https://wiki.postgresql.org/wiki/Tuning_Your_PostgreSQL_Server) |
| 74 | +formulas. For the `shared_buffers` we use 1/4 of given memory and for the |
| 75 | +`effective_cache_size` we set the value to 1/2 of the given memory. |
| 76 | + |
| 77 | +PostgreSQL admin account |
| 78 | +------------------------ |
| 79 | +The admin account `postgres` has no password set by default, only allowing local |
| 80 | +connections. You can set it by setting the `POSTGRESQL_ADMIN_PASSWORD` environment |
| 81 | +variable when initializing your container. This will allow you to login to the |
| 82 | +`postgres` account remotely. Local connections will still not require a password. |
| 83 | + |
| 84 | + |
| 85 | +Changing passwords |
| 86 | +------------------ |
| 87 | + |
| 88 | +Since passwords are part of the image configuration, the only supported method |
| 89 | +to change passwords for the database user (`POSTGRESQL_USER`) and `postgres` |
| 90 | +admin user is by changing the environment variables `POSTGRESQL_PASSWORD` and |
| 91 | +`POSTGRESQL_ADMIN_PASSWORD`, respectively. |
| 92 | + |
| 93 | +Changing database passwords through SQL statements or any way other than through |
| 94 | +the environment variables aforementioned will cause a mismatch between the |
| 95 | +values stored in the variables and the actual passwords. Whenever a database |
| 96 | +container starts it will reset the passwords to the values stored in the |
| 97 | +environment variables. |
| 98 | + |
| 99 | + |
| 100 | +Upgrading database (by switching to newer PostgreSQL image version) |
| 101 | +------------------------------------------------------------------- |
| 102 | + |
| 103 | +** Warning! Please, before you decide to do the data directory upgrade, always |
| 104 | +ensure that you've carefully backed up all your data and that you are OK with |
| 105 | +potential manual rollback! ** |
| 106 | + |
| 107 | +This image supports automatic upgrade of data directory created by |
| 108 | +the PostgreSQL server version 9.4 (and _only_ this version) - provided by sclorg |
| 109 | +image. The upgrade process is designed so that you should be able to just |
| 110 | +switch from *image A* to *image B*, and set the `$POSTGRESQL_UPGRADE` variable |
| 111 | +appropriately to explicitly request the database data transformation. |
| 112 | + |
| 113 | +The upgrade process is internally implemented via `pg_upgrade` binary, and for |
| 114 | +that purpose the container needs to contain two versions of PostgreSQL server |
| 115 | +(have a look at `man pg_upgrade` for more info). |
| 116 | + |
| 117 | +For the `pg_upgrade` process - and the new server version, we need to initialize |
| 118 | +a brand new data directory. That's data directory is created automatically by |
| 119 | +container tooling under /var/lib/pgsql/data, which is usually external |
| 120 | +bind-mountpoint. The `pg_upgrade` execution is then similar to dump&restore |
| 121 | +approach -- it starts both old and new PostgreSQL servers (within container) and |
| 122 | +"dumps" the old datadir while and at the same time it "restores" it into new |
| 123 | +datadir. This operation requires a lot of data files copying, so you can decide |
| 124 | +what type of upgrade you'll do by setting `$POSTGRESQL_UPGRADE` appropriately: |
| 125 | + |
| 126 | +| Variable value | Description | |
| 127 | +| :----------------- | ---------------------------------------------- | |
| 128 | +| `copy` | The data files are copied from old datadir to new datadir. This option has low risk of data loss in case of some upgrade failure. | |
| 129 | +| `hardlink` | Data files are hard-linked from old to the new data directory, which brings performance optimization - but the old directory becomes unusable, even in case of failure. | |
| 130 | + |
| 131 | +Note that because we copy data directory, you need to make sure that you have |
| 132 | +enough space for the copy; upgrade failure because of not enough space might |
| 133 | +lead to data loss. |
0 commit comments