mirror of
https://github.com/wassname/docker-postgresql.git
synced 2026-08-15 12:24:55 +08:00
@@ -5,6 +5,7 @@
|
|||||||
- [Reporting Issues](#reporting-issues)
|
- [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)
|
||||||
@@ -91,6 +92,41 @@ 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:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
**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: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.
|
||||||
|
|
||||||
|
For example,
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker run --name postgresql -d \
|
||||||
|
-e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' -e 'DB_NAME=dbname' \
|
||||||
|
sameersbn/postgresql:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
, 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
|
||||||
|
|||||||
@@ -6,6 +6,10 @@ 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="/var/lib/postgresql/${PG_VERSION}/main"
|
||||||
|
|
||||||
|
DB_NAME=${DB_NAME:-}
|
||||||
|
DB_USER=${DB_USER:-}
|
||||||
|
DB_PASS=${DB_PASS:-}
|
||||||
|
|
||||||
# fix permissions and ownership of /var/lib/postgresql
|
# fix permissions and ownership of /var/lib/postgresql
|
||||||
mkdir -p -m 0700 /var/lib/postgresql
|
mkdir -p -m 0700 /var/lib/postgresql
|
||||||
chown -R postgres:postgres /var/lib/postgresql
|
chown -R postgres:postgres /var/lib/postgresql
|
||||||
@@ -49,6 +53,35 @@ 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
|
||||||
|
echo "Creating database \"${DB_NAME}\"..."
|
||||||
|
echo "CREATE DATABASE ${DB_NAME};" | \
|
||||||
|
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_NAME}\" for user \"${DB_USER}\"..."
|
||||||
|
echo "GRANT ALL PRIVILEGES ON DATABASE ${DB_NAME} 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
|
||||||
|
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