mirror of
https://github.com/wassname/docker-postgresql.git
synced 2026-06-29 02:45:52 +08:00
Compare commits
45 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3230aafc6c | |||
| afdf196abc | |||
| 8a30ea03fc | |||
| f94ab9511e | |||
| df890a902f | |||
| 2af478b7cd | |||
| f75e33f072 | |||
| 65ad3beacf | |||
| a93afd52f8 | |||
| 66a27426e6 | |||
| 5a529a52ab | |||
| 2518d62378 | |||
| 1a7390c746 | |||
| 90f35e89b9 | |||
| e0478a049b | |||
| feecacfdf2 | |||
| df2638881b | |||
| cf2d42797b | |||
| 68977a5d19 | |||
| 8b52c4e35a | |||
| 22953b3498 | |||
| b2ab257dd6 | |||
| d07407dd75 | |||
| ef07c65029 | |||
| b3ca9b453b | |||
| 7bec626f6a | |||
| cc065698f1 | |||
| 6a4fe7ca5b | |||
| 370e1aea63 | |||
| 916a36f163 | |||
| c590589aab | |||
| 534854552d | |||
| 737747191f | |||
| 66b92b361c | |||
| 92e7c5c8c2 | |||
| c3b94e501d | |||
| d33c7cb512 | |||
| abcbb9b17d | |||
| 81786d6f16 | |||
| e0ce5c7005 | |||
| a37704e05e | |||
| 5023375538 | |||
| 4083c2af1c | |||
| cc675068ef | |||
| 1c866afa02 |
@@ -0,0 +1,4 @@
|
|||||||
|
.git
|
||||||
|
VERSION
|
||||||
|
README.md
|
||||||
|
Changelog.md
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
# Changelog
|
||||||
|
|
||||||
|
**9.4**
|
||||||
|
- postgresql: upgrade to 9.4
|
||||||
|
|
||||||
|
**9.1-2**
|
||||||
|
- use the official postgresql apt repo
|
||||||
|
- feature: automatic data migration on upgrade
|
||||||
|
|
||||||
|
**9.1-1**
|
||||||
|
- upgrade to sameersbn/ubuntu:20141001, fixes shellshock
|
||||||
|
- support creation of users and databases at launch (`docker run`)
|
||||||
|
- mount volume at `/var/run/postgresql` allowing the postgresql unix socket to be exposed
|
||||||
|
|
||||||
|
**9.1**
|
||||||
|
- optimized image size by removing `/var/lib/apt/lists/*`.
|
||||||
|
- update to the sameersbn/ubuntu:12.04.20140818 baseimage
|
||||||
|
- removed use of supervisord
|
||||||
+11
-5
@@ -1,14 +1,20 @@
|
|||||||
FROM sameersbn/ubuntu:12.04.20140818
|
FROM sameersbn/ubuntu:14.04.20150120
|
||||||
MAINTAINER sameer@damagehead.com
|
MAINTAINER sameer@damagehead.com
|
||||||
|
|
||||||
RUN apt-get update && \
|
ENV PG_VERSION 9.4
|
||||||
apt-get install -y --no-install-recommends postgresql postgresql-client && \
|
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \
|
||||||
rm -rf /var/lib/postgresql && \
|
&& echo 'deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main' > /etc/apt/sources.list.d/pgdg.list \
|
||||||
rm -rf /var/lib/apt/lists/* # 20140818
|
&& apt-get update \
|
||||||
|
&& apt-get install -y postgresql-${PG_VERSION} postgresql-client-${PG_VERSION} pwgen \
|
||||||
|
&& rm -rf /var/lib/postgresql \
|
||||||
|
&& rm -rf /var/lib/apt/lists/* # 20141001
|
||||||
|
|
||||||
ADD start /start
|
ADD start /start
|
||||||
RUN chmod 755 /start
|
RUN chmod 755 /start
|
||||||
|
|
||||||
EXPOSE 5432
|
EXPOSE 5432
|
||||||
|
|
||||||
VOLUME ["/var/lib/postgresql"]
|
VOLUME ["/var/lib/postgresql"]
|
||||||
|
VOLUME ["/run/postgresql"]
|
||||||
|
|
||||||
CMD ["/start"]
|
CMD ["/start"]
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2014 Sameer Naik
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -1,22 +1,64 @@
|
|||||||
# Table of Contents
|
# Table of Contents
|
||||||
|
|
||||||
- [Introduction](#introduction)
|
- [Introduction](#introduction)
|
||||||
|
- [Changelog](Changelog.md)
|
||||||
|
- [Contributing](#contributing)
|
||||||
|
- [Reporting Issues](#reporting-issues)
|
||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
- [Quick Start](#quick-start)
|
- [Quick Start](#quick-start)
|
||||||
|
- [Creating User and Database at Launch](creating-user-and-database-at-launch)
|
||||||
- [Configuration](#configuration)
|
- [Configuration](#configuration)
|
||||||
- [Data Store](#data-store)
|
- [Data Store](#data-store)
|
||||||
- [Securing the server](#securing-the-server)
|
- [Securing the server](#securing-the-server)
|
||||||
|
- [Shell Access](#shell-access)
|
||||||
- [Upgrading](#upgrading)
|
- [Upgrading](#upgrading)
|
||||||
- [Issues](#issues)
|
|
||||||
|
|
||||||
# Introduction
|
# Introduction
|
||||||
|
|
||||||
Dockerfile to build a PostgreSQL container image which can be linked to other containers.
|
Dockerfile to build a PostgreSQL container image which can be linked to other containers.
|
||||||
|
|
||||||
|
# Contributing
|
||||||
|
|
||||||
|
If you find this image useful here's how you can help:
|
||||||
|
|
||||||
|
- Send a Pull Request with your awesome new features and bug fixes
|
||||||
|
- Help new users with [Issues](https://github.com/sameersbn/docker-postgresql/issues) they may encounter
|
||||||
|
- Send me a tip via [Bitcoin](https://www.coinbase.com/sameersbn) or using [Gratipay](https://gratipay.com/sameersbn/)
|
||||||
|
|
||||||
|
# Reporting Issues
|
||||||
|
|
||||||
|
Docker is a relatively new project and is active being developed and tested by a thriving community of developers and testers and every release of docker features many enhancements and bugfixes.
|
||||||
|
|
||||||
|
Given the nature of the development and release cycle it is very important that you have the latest version of docker installed because any issue that you encounter might have already been fixed with a newer docker release.
|
||||||
|
|
||||||
|
For ubuntu users I suggest [installing docker](https://docs.docker.com/installation/ubuntulinux/) using docker's own package repository since the version of docker packaged in the ubuntu repositories are a little dated.
|
||||||
|
|
||||||
|
Here is the shortform of the installation of an updated version of docker on ubuntu.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo apt-get purge docker.io
|
||||||
|
curl -s https://get.docker.io/ubuntu/ | sudo sh
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install lxc-docker
|
||||||
|
```
|
||||||
|
|
||||||
|
Fedora and RHEL/CentOS users should try disabling selinux with `setenforce 0` and check if resolves the issue. If it does than there is not much that I can help you with. You can either stick with selinux disabled (not recommended by redhat) or switch to using ubuntu.
|
||||||
|
|
||||||
|
If using the latest docker version and/or disabling selinux does not fix the issue then please file a issue request on the [issues](https://github.com/sameersbn/docker-postgresql/issues) page.
|
||||||
|
|
||||||
|
In your issue report please make sure you provide the following information:
|
||||||
|
|
||||||
|
- The host ditribution and release version.
|
||||||
|
- Output of the `docker version` command
|
||||||
|
- Output of the `docker info` command
|
||||||
|
- The `docker run` command you used to run the image (mask out the sensitive bits).
|
||||||
|
|
||||||
# Installation
|
# Installation
|
||||||
|
|
||||||
Pull the latest version of the image from the docker index. This is the recommended method of installation as it is easier to update image in the future. These builds are performed by the **Docker Trusted Build** service.
|
Pull the latest version of the image from the docker index. This is the recommended method of installation as it is easier to update image in the future. These builds are performed by the **Docker Trusted Build** service.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker pull sameersbn/postgresql:latest
|
docker pull sameersbn/postgresql:9.4
|
||||||
```
|
```
|
||||||
|
|
||||||
Alternately you can build the image yourself.
|
Alternately you can build the image yourself.
|
||||||
@@ -28,19 +70,28 @@ docker build -t="$USER/postgresql" .
|
|||||||
```
|
```
|
||||||
|
|
||||||
# Quick Start
|
# Quick Start
|
||||||
|
|
||||||
Run the postgresql image
|
Run the postgresql image
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run --name postgresql -d sameersbn/postgresql:latest
|
docker run --name postgresql -d sameersbn/postgresql:9.4
|
||||||
```
|
```
|
||||||
|
|
||||||
By default remote logins are permitted to the postgresql server and a random password is assigned for the postgres user. The password set for the postgres user can be retrieved from the container logs.
|
The simplest way to login to the postgresql container as the administrative `postgres` user is to use the `--volumes-from` docker option to connect to the postgresql server over the unix socket.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run -it --rm --volumes-from=postgresql \
|
||||||
|
sameersbn/postgresql:9.4 sudo -u postgres -H psql
|
||||||
|
```
|
||||||
|
|
||||||
|
Alternately you can fetch the password set for the `postgres` user from the container logs.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker logs postgresql
|
docker logs postgresql
|
||||||
```
|
```
|
||||||
|
|
||||||
In the output you will notice the following lines with the password:
|
In the output you will notice the following lines with the password:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|------------------------------------------------------------------|
|
|------------------------------------------------------------------|
|
||||||
| PostgreSQL User: postgres, Password: xxxxxxxxxxxxxx |
|
| PostgreSQL User: postgres, Password: xxxxxxxxxxxxxx |
|
||||||
@@ -57,23 +108,75 @@ To test if the postgresql server is working properly, try connecting to the serv
|
|||||||
psql -U postgres -h $(docker inspect --format {{.NetworkSettings.IPAddress}} postgresql)
|
psql -U postgres -h $(docker inspect --format {{.NetworkSettings.IPAddress}} postgresql)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# Creating User and Database at Launch
|
||||||
|
|
||||||
|
The image allows you to create a user and database at launch time.
|
||||||
|
|
||||||
|
To create a new user you should specify the `DB_USER` and `DB_PASS` variables. The following command will create a new user *dbuser* with the password *dbpass*.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --name postgresql -d \
|
||||||
|
-e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' \
|
||||||
|
sameersbn/postgresql:9.4
|
||||||
|
```
|
||||||
|
|
||||||
|
**NOTE**
|
||||||
|
- If the password is not specified the user will not be created
|
||||||
|
- If the user user already exists no changes will be made
|
||||||
|
|
||||||
|
Similarly, you can also create a new database by specifying the database name in the `DB_NAME` variable.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --name postgresql -d \
|
||||||
|
-e 'DB_NAME=dbname' sameersbn/postgresql:9.4
|
||||||
|
```
|
||||||
|
|
||||||
|
You may also specify a comma separated list of database names in the `DB_NAME` variable. The following command creates two new databases named *dbname1* and *dbname2 (p.s. this feature is only available in releases greater than 9.4)*
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --name postgresql -d \
|
||||||
|
-e 'DB_NAME=dbname1,dbname2' sameersbn/postgresql:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
If the `DB_USER` and `DB_PASS` variables are also specified while creating the database, then the user is granted access to the database(s).
|
||||||
|
|
||||||
|
For example,
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --name postgresql -d \
|
||||||
|
-e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' -e 'DB_NAME=dbname' \
|
||||||
|
sameersbn/postgresql:9.4
|
||||||
|
```
|
||||||
|
|
||||||
|
, will create a user *dbuser* with the password *dbpass*. It will also create a database named *dbname* and the *dbuser* user will have full access to the *dbname* database.
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
|
|
||||||
## Data Store
|
## Data Store
|
||||||
For data persistence a volume should be mounted at /var/lib/postgresql.
|
|
||||||
|
For data persistence a volume should be mounted at `/var/lib/postgresql`.
|
||||||
|
|
||||||
|
SELinux users are also required to change the security context of the mount point so that it plays nicely with selinux.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mkdir -p /opt/postgresql/data
|
||||||
|
sudo chcon -Rt svirt_sandbox_file_t /opt/postgresql/data
|
||||||
|
```
|
||||||
|
|
||||||
|
The updated run command looks like this.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
mkdir /opt/postgresql/data
|
|
||||||
docker run --name postgresql -d \
|
docker run --name postgresql -d \
|
||||||
-v /opt/postgresql/data:/var/lib/postgresql sameersbn/postgresql:latest
|
-v /opt/postgresql/data:/var/lib/postgresql sameersbn/postgresql:9.4
|
||||||
```
|
```
|
||||||
|
|
||||||
This will make sure that the data stored in the database is not lost when the image is stopped and started again.
|
This will make sure that the data stored in the database is not lost when the image is stopped and started again.
|
||||||
|
|
||||||
## Securing the server
|
## Securing the server
|
||||||
By default a randomly generated password is assigned for the postgres user. The password is stored in a file named pwpass in the data store and is printed in the logs.
|
|
||||||
|
|
||||||
If you dont want this password to be displayed in the logs, then please note down the password listed in /opt/postgresql/data/pwpass and then delete the file.
|
By default a randomly generated password is assigned for the postgres user. The password is stored in a file named `pwfile` in the data store and is printed in the logs.
|
||||||
|
|
||||||
|
If you dont want this password to be displayed in the logs, then please note down the password listed in `/opt/postgresql/data/pwfile` and then delete the file.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
cat /opt/postgresql/data/pwfile
|
cat /opt/postgresql/data/pwfile
|
||||||
@@ -87,6 +190,32 @@ psql -U postgres -h $(docker inspect --format {{.NetworkSettings.IPAddress}} pos
|
|||||||
\password postgres
|
\password postgres
|
||||||
```
|
```
|
||||||
|
|
||||||
|
# Shell Access
|
||||||
|
|
||||||
|
For debugging and maintenance purposes you may want access the containers shell. If you are using docker version `1.3.0` or higher you can access a running containers shell using `docker exec` command.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker exec -it postgresql bash
|
||||||
|
```
|
||||||
|
|
||||||
|
If you are using an older version of docker, you can use the [nsenter](http://man7.org/linux/man-pages/man1/nsenter.1.html) linux tool (part of the util-linux package) to access the container shell.
|
||||||
|
|
||||||
|
Some linux distros (e.g. ubuntu) use older versions of the util-linux which do not include the `nsenter` tool. To get around this @jpetazzo has created a nice docker image that allows you to install the `nsenter` utility and a helper script named `docker-enter` on these distros.
|
||||||
|
|
||||||
|
To install `nsenter` execute the following command on your host,
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
|
||||||
|
```
|
||||||
|
|
||||||
|
Now you can access the container shell using the command
|
||||||
|
|
||||||
|
```bash
|
||||||
|
sudo docker-enter postgresql
|
||||||
|
```
|
||||||
|
|
||||||
|
For more information refer https://github.com/jpetazzo/nsenter
|
||||||
|
|
||||||
# Upgrading
|
# Upgrading
|
||||||
|
|
||||||
To upgrade to newer releases, simply follow this 3 step upgrade procedure.
|
To upgrade to newer releases, simply follow this 3 step upgrade procedure.
|
||||||
@@ -100,14 +229,11 @@ docker stop postgresql
|
|||||||
- **Step 2**: Update the docker image.
|
- **Step 2**: Update the docker image.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker pull sameersbn/postgresql:latest
|
docker pull sameersbn/postgresql:9.4
|
||||||
```
|
```
|
||||||
|
|
||||||
- **Step 3**: Start the image
|
- **Step 3**: Start the image
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
docker run --name postgresql -d [OPTIONS] sameersbn/postgresql:latest
|
docker run --name postgresql -d [OPTIONS] sameersbn/postgresql:9.4
|
||||||
```
|
```
|
||||||
|
|
||||||
# Issues
|
|
||||||
Please report issues [here](https://github.com/sameersbn/docker-postgresql/issues)
|
|
||||||
|
|||||||
@@ -1,14 +1,23 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
PG_VERSION="9.1"
|
PG_HOME="/var/lib/postgresql"
|
||||||
PG_CONFDIR="/etc/postgresql/${PG_VERSION}/main"
|
PG_CONFDIR="/etc/postgresql/${PG_VERSION}/main"
|
||||||
PG_BINDIR="/usr/lib/postgresql/${PG_VERSION}/bin"
|
PG_BINDIR="/usr/lib/postgresql/${PG_VERSION}/bin"
|
||||||
PG_DATADIR="/var/lib/postgresql/${PG_VERSION}/main"
|
PG_DATADIR="${PG_HOME}/${PG_VERSION}/main"
|
||||||
|
|
||||||
# fix permissions and ownership of /var/lib/postgresql
|
DB_NAME=${DB_NAME:-}
|
||||||
chown -R postgres:postgres /var/lib/postgresql
|
DB_USER=${DB_USER:-}
|
||||||
chmod 700 /var/lib/postgresql
|
DB_PASS=${DB_PASS:-}
|
||||||
|
|
||||||
|
# fix permissions and ownership of ${PG_HOME}
|
||||||
|
mkdir -p -m 0700 ${PG_HOME}
|
||||||
|
chown -R postgres:postgres ${PG_HOME}
|
||||||
|
|
||||||
|
# fix permissions and ownership of /run/postgresql
|
||||||
|
mkdir -p -m 0755 /run/postgresql /run/postgresql/${PG_VERSION}-main.pg_stat_tmp
|
||||||
|
chown -R postgres:postgres /run/postgresql
|
||||||
|
chmod g+s /run/postgresql
|
||||||
|
|
||||||
# disable ssl
|
# disable ssl
|
||||||
sed 's/ssl = true/#ssl = true/' -i ${PG_CONFDIR}/postgresql.conf
|
sed 's/ssl = true/#ssl = true/' -i ${PG_CONFDIR}/postgresql.conf
|
||||||
@@ -23,18 +32,51 @@ cat >> ${PG_CONFDIR}/pg_hba.conf <<EOF
|
|||||||
host all all 0.0.0.0/0 md5
|
host all all 0.0.0.0/0 md5
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
cd ${PG_HOME}
|
||||||
|
|
||||||
# initialize PostgreSQL data directory
|
# initialize PostgreSQL data directory
|
||||||
if [ ! -d ${PG_DATADIR} ]; then
|
if [ ! -d ${PG_DATADIR} ]; then
|
||||||
|
# check if we need to perform data migration
|
||||||
|
PG_OLD_VERSION=$(find ${PG_HOME}/[0-9].[0-9]/main -maxdepth 1 -name PG_VERSION | sort -r | head -n1 | cut -d'/' -f5)
|
||||||
|
|
||||||
|
if [ ! -f "${PG_HOME}/pwfile" ]; then
|
||||||
|
PG_PASSWORD=$(pwgen -c -n -1 14)
|
||||||
|
echo "${PG_PASSWORD}" > ${PG_HOME}/pwfile
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Initializing database..."
|
echo "Initializing database..."
|
||||||
PG_PASSWORD=$(pwgen -c -n -1 14)
|
|
||||||
echo "${PG_PASSWORD}" > /var/lib/postgresql/pwfile
|
|
||||||
sudo -u postgres -H "${PG_BINDIR}/initdb" \
|
sudo -u postgres -H "${PG_BINDIR}/initdb" \
|
||||||
--pgdata="${PG_DATADIR}" --pwfile=/var/lib/postgresql/pwfile \
|
--pgdata="${PG_DATADIR}" --pwfile=${PG_HOME}/pwfile \
|
||||||
--username=postgres --encoding=unicode --auth=trust >/dev/null
|
--username=postgres --encoding=unicode --auth=trust >/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ -f /var/lib/postgresql/pwfile ]; then
|
if [ -n "${PG_OLD_VERSION}" ]; then
|
||||||
PG_PASSWORD=$(cat /var/lib/postgresql/pwfile)
|
echo "Migrating postgresql ${PG_OLD_VERSION} data..."
|
||||||
|
PG_OLD_CONFDIR="/etc/postgresql/${PG_OLD_VERSION}/main"
|
||||||
|
PG_OLD_BINDIR="/usr/lib/postgresql/${PG_OLD_VERSION}/bin"
|
||||||
|
PG_OLD_DATADIR="${PG_HOME}/${PG_OLD_VERSION}/main"
|
||||||
|
|
||||||
|
# backup ${PG_OLD_DATADIR} to avoid data loss
|
||||||
|
PG_BKP_SUFFIX=$(date +%Y%m%d%H%M%S)
|
||||||
|
echo "Backing up ${PG_OLD_DATADIR} to ${PG_OLD_DATADIR}.${PG_BKP_SUFFIX}..."
|
||||||
|
cp -a ${PG_OLD_DATADIR} ${PG_OLD_DATADIR}.${PG_BKP_SUFFIX}
|
||||||
|
|
||||||
|
echo "Installing postgresql-${PG_OLD_VERSION}..."
|
||||||
|
apt-get update
|
||||||
|
apt-get install postgresql-${PG_OLD_VERSION} postgresql-client-${PG_OLD_VERSION}
|
||||||
|
rm -rf /var/lib/apt/lists/*
|
||||||
|
|
||||||
|
# migrate ${PG_OLD_VERSION} data
|
||||||
|
echo "Migration in progress. This could take a while, please be patient..."
|
||||||
|
sudo -u postgres -H ${PG_BINDIR}/pg_upgrade \
|
||||||
|
-b ${PG_OLD_BINDIR} -B ${PG_BINDIR} \
|
||||||
|
-d ${PG_OLD_DATADIR} -D ${PG_DATADIR} \
|
||||||
|
-o "-c config_file=${PG_OLD_CONFDIR}/postgresql.conf" \
|
||||||
|
-O "-c config_file=${PG_CONFDIR}/postgresql.conf" >/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f ${PG_HOME}/pwfile ]; then
|
||||||
|
PG_PASSWORD=$(cat ${PG_HOME}/pwfile)
|
||||||
echo "|------------------------------------------------------------------|"
|
echo "|------------------------------------------------------------------|"
|
||||||
echo "| PostgreSQL User: postgres, Password: ${PG_PASSWORD} |"
|
echo "| PostgreSQL User: postgres, Password: ${PG_PASSWORD} |"
|
||||||
echo "| |"
|
echo "| |"
|
||||||
@@ -44,6 +86,37 @@ if [ -f /var/lib/postgresql/pwfile ]; then
|
|||||||
echo "|------------------------------------------------------------------|"
|
echo "|------------------------------------------------------------------|"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
if [ -n "${DB_USER}" ]; then
|
||||||
|
if [ -z "${DB_PASS}" ]; then
|
||||||
|
echo ""
|
||||||
|
echo "WARNING: "
|
||||||
|
echo " Please specify a password for \"${DB_USER}\". Skipping user creation..."
|
||||||
|
echo ""
|
||||||
|
DB_USER=
|
||||||
|
else
|
||||||
|
echo "Creating user \"${DB_USER}\"..."
|
||||||
|
echo "CREATE ROLE ${DB_USER} with LOGIN CREATEDB PASSWORD '${DB_PASS}';" |
|
||||||
|
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
|
||||||
|
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null 2>&1
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "${DB_NAME}" ]; then
|
||||||
|
for db in $(awk -F',' '{for (i = 1 ; i <= NF ; i++) print $i}' <<< "${DB_NAME}"); do
|
||||||
|
echo "Creating database \"${db}\"..."
|
||||||
|
echo "CREATE DATABASE ${db};" | \
|
||||||
|
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
|
||||||
|
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null 2>&1
|
||||||
|
|
||||||
|
if [ -n "${DB_USER}" ]; then
|
||||||
|
echo "Granting access to database \"${db}\" for user \"${DB_USER}\"..."
|
||||||
|
echo "GRANT ALL PRIVILEGES ON DATABASE ${db} to ${DB_USER};" |
|
||||||
|
sudo -u postgres -H ${PG_BINDIR}/postgres --single \
|
||||||
|
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf >/dev/null 2>&1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
echo "Starting PostgreSQL server..."
|
echo "Starting PostgreSQL server..."
|
||||||
exec sudo -u postgres -H ${PG_BINDIR}/postgres \
|
exec sudo -u postgres -H ${PG_BINDIR}/postgres \
|
||||||
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf
|
-D ${PG_DATADIR} -c config_file=${PG_CONFDIR}/postgresql.conf
|
||||||
|
|||||||
Reference in New Issue
Block a user