Skip to content

Commit 6818374

Browse files
committed
add root as a copy
1 parent 294af7c commit 6818374

File tree

14 files changed

+821
-1
lines changed

14 files changed

+821
-1
lines changed

postgresql-9.6/docker/root

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#!/usr/bin/python
2+
3+
"""
4+
Script for parsing cgroup information
5+
6+
This script will read some limits from the cgroup system and parse
7+
them, printing out "VARIABLE=VALUE" on each line for every limit that is
8+
successfully read. Output of this script can be directly fed into
9+
bash's export command. Recommended usage from a bash script:
10+
11+
set -o errexit
12+
export_vars=$(cgroup-limits) ; export $export_vars
13+
14+
Variables currently supported:
15+
MAX_MEMORY_LIMIT_IN_BYTES
16+
Maximum possible limit MEMORY_LIMIT_IN_BYTES can have. This is
17+
currently constant value of 9223372036854775807.
18+
MEMORY_LIMIT_IN_BYTES
19+
Maximum amount of user memory in bytes. If this value is set
20+
to the same value as MAX_MEMORY_LIMIT_IN_BYTES, it means that
21+
there is no limit set. The value is taken from
22+
/sys/fs/cgroup/memory/memory.limit_in_bytes
23+
NUMBER_OF_CORES
24+
Number of detected CPU cores that can be used. This value is
25+
calculated from /sys/fs/cgroup/cpuset/cpuset.cpus
26+
NO_MEMORY_LIMIT
27+
Set to "true" if MEMORY_LIMIT_IN_BYTES is so high that the caller
28+
can act as if no memory limit was set. Undefined otherwise.
29+
"""
30+
31+
from __future__ import print_function
32+
import sys
33+
34+
35+
def _read_file(path):
36+
try:
37+
with open(path, 'r') as f:
38+
return f.read().strip()
39+
except IOError:
40+
return None
41+
42+
43+
def get_memory_limit():
44+
"""
45+
Read memory limit, in bytes.
46+
"""
47+
48+
limit = _read_file('/sys/fs/cgroup/memory/memory.limit_in_bytes')
49+
if limit is None or not limit.isdigit():
50+
print("Warning: Can't detect memory limit from cgroups",
51+
file=sys.stderr)
52+
return None
53+
return int(limit)
54+
55+
56+
def get_number_of_cores():
57+
"""
58+
Read number of CPU cores.
59+
"""
60+
61+
core_count = 0
62+
63+
line = _read_file('/sys/fs/cgroup/cpuset/cpuset.cpus')
64+
if line is None:
65+
print("Warning: Can't detect number of CPU cores from cgroups",
66+
file=sys.stderr)
67+
return None
68+
69+
for group in line.split(','):
70+
core_ids = list(map(int, group.split('-')))
71+
if len(core_ids) == 2:
72+
core_count += core_ids[1] - core_ids[0] + 1
73+
else:
74+
core_count += 1
75+
76+
return core_count
77+
78+
79+
if __name__ == "__main__":
80+
env_vars = {
81+
"MAX_MEMORY_LIMIT_IN_BYTES": 9223372036854775807,
82+
"MEMORY_LIMIT_IN_BYTES": get_memory_limit(),
83+
"NUMBER_OF_CORES": get_number_of_cores()
84+
}
85+
86+
env_vars = {k: v for k, v in env_vars.items() if v is not None}
87+
88+
if env_vars.get("MEMORY_LIMIT_IN_BYTES", 0) >= 92233720368547:
89+
env_vars["NO_MEMORY_LIMIT"] = "true"
90+
91+
for key, value in env_vars.items():
92+
print("{0}={1}".format(key, value))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
exec "$@"
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
3+
export ENABLE_REPLICATION=${ENABLE_REPLICATION:-false}
4+
5+
set -eu
6+
export_vars=$(cgroup-limits) ; export $export_vars
7+
8+
source "${CONTAINER_SCRIPTS_PATH}/common.sh"
9+
10+
set_pgdata
11+
check_env_vars
12+
generate_passwd_file
13+
generate_postgresql_config
14+
15+
# Is this brand new data volume?
16+
PG_INITIALIZED=false
17+
18+
if [ ! -f "$PGDATA/postgresql.conf" ]; then
19+
initialize_database
20+
PG_INITIALIZED=:
21+
else
22+
try_pgupgrade
23+
fi
24+
25+
pg_ctl -w start -o "-h ''"
26+
if $PG_INITIALIZED ; then
27+
create_users
28+
fi
29+
30+
if [ ! -f "$PGDATA/fdw.conf" ]; then
31+
create_fdw
32+
fi
33+
34+
create_postgis_pgcrypto
35+
set_passwords
36+
pg_ctl stop
37+
38+
unset_env_vars
39+
exec postgres "$@"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
3+
export ENABLE_REPLICATION=true
4+
5+
exec run-postgresql "$@"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
export ENABLE_REPLICATION=true
4+
5+
set -eu
6+
export_vars=$(cgroup-limits) ; export $export_vars
7+
8+
source "$CONTAINER_SCRIPTS_PATH"/common.sh
9+
10+
set_pgdata
11+
12+
function initialize_replica() {
13+
echo "Initializing PostgreSQL slave ..."
14+
# TODO: Validate and reuse existing data?
15+
rm -rf $PGDATA
16+
PGPASSWORD="${POSTGRESQL_MASTER_PASSWORD}" pg_basebackup -x --no-password --pgdata ${PGDATA} --host=${MASTER_FQDN} --port=5432 -U "${POSTGRESQL_MASTER_USER}"
17+
18+
# PostgreSQL recovery configuration.
19+
generate_postgresql_recovery_config
20+
cat >> "$PGDATA/recovery.conf" <<EOF
21+
22+
# Custom OpenShift recovery configuration:
23+
include '${POSTGRESQL_RECOVERY_FILE}'
24+
EOF
25+
}
26+
27+
check_env_vars
28+
generate_passwd_file
29+
generate_postgresql_config
30+
31+
wait_for_postgresql_master
32+
export MASTER_FQDN=$(postgresql_master_addr)
33+
initialize_replica
34+
35+
unset_env_vars
36+
exec postgres "$@"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
# Fix permissions on the given directory to allow group read/write of
3+
# regular files and execute of directories.
4+
find "$1" -exec chown postgres {} \;
5+
find "$1" -exec chgrp 0 {} \;
6+
find "$1" -exec chmod g+rw {} \;
7+
find "$1" -type d -exec chmod g+x {} +
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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

Comments
 (0)