Sign inSign up

ashraj1/postgresql

By ashraj1

•Updated about 6 years ago

This is for Postgresql database as Master/slave

Image
0

1.1K

ashraj1/postgresql repository overview

⁠What is PostgreSQL?

PostgreSQL⁠ is an object-relational database management system (ORDBMS) with an emphasis on extensibility and on standards-compliance [source]⁠.

⁠TL;DR

$ docker run --name postgresql bitnami/postgresql:latest

⁠Docker Compose

$ curl -sSL https://raw.githubusercontent.com/bitnami/bitnami-docker-postgresql/master/docker-compose.yml > docker-compose.yml
$ docker-compose up -d

⁠Why use Bitnami Images?

  • Bitnami closely tracks upstream source changes and promptly publishes new versions of this image using our automated systems.
  • With Bitnami images the latest bug fixes and features are available as soon as possible.
  • Bitnami containers, virtual machines and cloud images use the same components and configuration approach - making it easy to switch between formats based on your project needs.
  • All our images are based on minideb⁠ a minimalist Debian based container image which gives you a small base container image and the familiarity of a leading Linux distribution.
  • All Bitnami images available in Docker Hub are signed with Docker Content Trust (DCT)⁠. You can use DOCKER_CONTENT_TRUST=1 to verify the integrity of the images.
  • Bitnami container images are released daily with the latest distribution packages available.

This CVE scan report⁠ contains a security report with all open CVEs. To get the list of actionable security issues, find the "latest" tag, click the vulnerability report link under the corresponding "Security scan" field and then select the "Only show fixable" filter on the next page.

⁠How to deploy PostgreSQL in Kubernetes?

Deploying Bitnami applications as Helm Charts is the easiest way to get started with our applications on Kubernetes. Read more about the installation in the Bitnami PostgreSQL Chart GitHub repository⁠.

Bitnami containers can be used with Kubeapps⁠ for deployment and management of Helm Charts in clusters.

⁠Why use a non-root container?

Non-root container images add an extra layer of security and are generally recommended for production environments. However, because they run as a non-root user, privileged tasks are typically off-limits. Learn more about non-root containers in our docs⁠.

Learn more about the Bitnami tagging policy and the difference between rolling tags and immutable tags in our documentation page⁠.

Subscribe to project updates by watching the bitnami/postgresql GitHub repo⁠.

⁠Get this image

The recommended way to get the Bitnami PostgreSQL Docker Image is to pull the prebuilt image from the Docker Hub Registry⁠.

$ docker pull bitnami/postgresql:latest

To use a specific version, you can pull a versioned tag. You can view the list of available versions⁠ in the Docker Hub Registry.

$ docker pull bitnami/postgresql:[TAG]

If you wish, you can also build the image yourself.

$ docker build -t bitnami/postgresql:latest 'https://github.com/bitnami/bitnami-docker-postgresql.git#master:11/debian-10'

⁠Persisting your database

If you remove the container all your data and configurations will be lost, and the next time you run the image the database will be reinitialized. To avoid this loss of data, you should mount a volume that will persist even after the container is removed.

For persistence you should mount a directory at the /bitnami/postgresql path. If the mounted directory is empty, it will be initialized on the first run.

$ docker run \
    -v /path/to/postgresql-persistence:/bitnami/postgresql \
    bitnami/postgresql:latest

or by modifying the docker-compose.yml⁠ file present in this repository:

services:
  postgresql:
  ...
    volumes:
      - /path/to/postgresql-persistence:/bitnami/postgresql
  ...

NOTE: As this is a non-root container, the mounted files and directories must have the proper permissions for the UID 1001.

⁠Connecting to other containers

Using Docker container networking⁠, a PostgreSQL server running inside a container can easily be accessed by your application containers.

Containers attached to the same network can communicate with each other using the container name as the hostname.

⁠Using the Command Line

In this example, we will create a PostgreSQL client instance that will connect to the server instance that is running on the same docker network as the client.

⁠Step 1: Create a network
$ docker network create app-tier --driver bridge
⁠Step 2: Launch the PostgreSQL server instance

Use the --network app-tier argument to the docker run command to attach the PostgreSQL container to the app-tier network.

$ docker run -d --name postgresql-server \
    --network app-tier \
    bitnami/postgresql:latest
⁠Step 3: Launch your PostgreSQL client instance

Finally we create a new container instance to launch the PostgreSQL client and connect to the server created in the previous step:

$ docker run -it --rm \
    --network app-tier \
    bitnami/postgresql:latest psql -h postgresql-server -U postgres

⁠Using Docker Compose

When not specified, Docker Compose automatically sets up a new network and attaches all deployed services to that network. However, we will explicitly define a new bridge network named app-tier. In this example we assume that you want to connect to the PostgreSQL server from your own custom application image which is identified in the following snippet by the service name myapp.

version: '2'

networks:
  app-tier:
    driver: bridge

services:
  postgresql:
    image: 'bitnami/postgresql:latest'
    networks:
      - app-tier
  myapp:
    image: 'YOUR_APPLICATION_IMAGE'
    networks:
      - app-tier

IMPORTANT:

  1. Please update the YOUR_APPLICATION_IMAGE_ placeholder in the above snippet with your application image
  2. In your application container, use the hostname postgresql to connect to the PostgreSQL server

Launch the containers using:

$ docker-compose up -d

⁠Configuration

⁠On container start

When the container is executed, it will execute the files with extension .sh located at /docker-entrypoint-preinitdb.d before initializing or starting postgresql.

In order to have your custom files inside the docker image you can mount them as a volume.

⁠Initializing a new instance

When the container is executed for the first time, it will execute the files with extensions .sh, .sql and .sql.gz located at /docker-entrypoint-initdb.d.

In order to have your custom files inside the docker image you can mount them as a volume.

⁠Setting the root password on first run

In the above commands you may have noticed the use of the POSTGRESQL_PASSWORD environment variable. Passing the POSTGRESQL_PASSWORD environment variable when running the image for the first time will set the password of the postgres user to the value of POSTGRESQL_PASSWORD (or the content of the file specified in POSTGRESQL_PASSWORD_FILE).

$ docker run --name postgresql -e POSTGRESQL_PASSWORD=password123 bitnami/postgresql:latest

or by modifying the docker-compose.yml⁠ file present in this repository:

services:
  postgresql:
  ...
    environment:
      - POSTGRESQL_PASSWORD=password123
  ...

Note! The postgres user is a superuser and has full administrative access to the PostgreSQL database.

Refer to Creating a database user on first run⁠ if you want to set an unprivileged user and a password for the postgres user.

⁠Creating a database on first run

By passing the POSTGRESQL_DATABASE environment variable when running the image for the first time, a database will be created. This is useful if your application requires that a database already exists, saving you from having to manually create the database using the PostgreSQL client.

$ docker run --name postgresql -e POSTGRESQL_DATABASE=my_database bitnami/postgresql:latest

or by modifying the docker-compose.yml⁠ file present in this repository:

services:
  postgresql:
  ...
    environment:
      - POSTGRESQL_DATABASE=my_database
  ...

⁠Creating a database user on first run

You can also create a restricted database user that only has permissions for the database created with the POSTGRESQL_DATABASE⁠ environment variable. To do this, provide the POSTGRESQL_USERNAME environment variable.

$ docker run --name postgresql -e POSTGRESQL_USERNAME=my_user -e POSTGRESQL_PASSWORD=password123 -e POSTGRESQL_DATABASE=my_database bitnami/postgresql:latest

or by modifying the docker-compose.yml⁠ file present in this repository:

services:
  postgresql:
  ...
    environment:
      - POSTGRESQL_USERNAME=my_user
      - POSTGRESQL_PASSWORD=password123
      - POSTGRESQL_DATABASE=my_database
  ...

Note! When POSTGRESQL_USERNAME is specified, the postgres user is not assigned a password and as a result you cannot login remotely to the PostgreSQL server as the postgres user. If you still want to have access with the user postgres, please set the POSTGRESQL_POSTGRES_PASSWORD environment variable (or the content of the file specified in POSTGRESQL_POSTGRES_PASSWORD_FILE).

⁠Setting up a streaming replication

A Streaming replication⁠ cluster can easily be setup with the Bitnami PostgreSQL Docker Image using the following environment variables:

  • POSTGRESQL_REPLICATION_MODE: Replication mode. Possible values master/slave. No defaults.
  • POSTGRESQL_REPLICATION_USER: The replication user created on the master on first run. No defaults.
  • POSTGRESQL_REPLICATION_PASSWORD: The replication users password. No defaults.
  • POSTGRESQL_REPLICATION_PASSWORD_FILE: Path to a file that contains the replication users password. This will override the value specified in POSTGRESQL_REPLICATION_PASSWORD. No defaults.
  • POSTGRESQL_MASTER_HOST: Hostname/IP of replication master (slave parameter). No defaults.
  • POSTGRESQL_MASTER_PORT_NUMBER: Server port of the replication master (slave parameter). Defaults to 5432.

In a replication cluster you can have one master and zero or more slaves. When replication is enabled the master node is in read-write mode, while the slaves are in read-only mode. For best performance its advisable to limit the reads to the slaves.

⁠Step 1: Create the replication master

The first step is to start the master.

$ docker run --name postgresql-master \
  -e POSTGRESQL_REPLICATION_MODE=master \
  -e POSTGRESQL_USERNAME=my_user \
  -e POSTGRESQL_PASSWORD=password123 \
  -e POSTGRESQL_DATABASE=my_database \
  -e POSTGRESQL_REPLICATION_USER=my_repl_user \
  -e POSTGRESQL_REPLICATION_PASSWORD=my_repl_password \
  bitnami/postgresql:latest

In this command we are configuring the container as the master using the POSTGRESQL_REPLICATION_MODE=master parameter. A replication user is specified using the POSTGRESQL_REPLICATION_USER and POSTGRESQL_REPLICATION_PASSWORD parameters.

⁠Step 2: Create the replication slave

Next we start a replication slave container.

$ docker run --name postgresql-slave \
  --link postgresql-master:master \
  -e POSTGRESQL_REPLICATION_MODE=slave \
  -e POSTGRESQL_MASTER_HOST=master \
  -e POSTGRESQL_MASTER_PORT_NUMBER=5432 \
  -e POSTGRESQL_REPLICATION_USER=my_repl_user \
  -e POSTGRESQL_REPLICATION_PASSWORD=my_repl_password \
  bitnami/postgresql:latest

In the above command the container is configured as a slave using the POSTGRESQL_REPLICATION_MODE parameter. Before the replication slave is started, the POSTGRESQL_MASTER_HOST and POSTGRESQL_MASTER_PORT_NUMBER parameters are used by the slave container to connect to the master and replicate the initial database from the master. The POSTGRESQL_REPLICATION_USER and POSTGRESQL_REPLICATION_PASSWORD credentials are used to authenticate with the master. In order to change the pg_hba.conf default settings, the slave needs to know if POSTGRESQL_PASSWORD is set.

With these two commands you now have a two node PostgreSQL master-slave streaming replication cluster up and running. You can scale the cluster by adding/removing slaves without incurring any downtime.

Note: The cluster replicates the master in its entirety, which includes all users and databases.

If the master goes down you can reconfigure a slave to act as the master and begin accepting writes by creating the trigger file /tmp/postgresql.trigger.5432. For example the following command reconfigures postgresql-slave to act as the master:

$ docker exec postgresql-slave touch /tmp/postgresql.trigger.5432

Note: The configuration of the other slaves in the cluster needs to be updated so that they are aware of the new master. This would require you to restart the other slaves with --link postgresql-slave:master as per our examples.

With Docker Compose the master-slave replication can be setup using:

version: '2'

services:
  postgresql-master:
    image: 'bitnami/postgresql:latest'
    ports:
      - '5432'
    volumes:
      - 'postgresql_master_data:/bitnami/postgresql'
    environment:
      - POSTGRESQL_REPLICATION_MODE=master
      - POSTGRESQL_REPLICATION_USER=repl_user
      - POSTGRESQL_REPLICATION_PASSWORD=repl_password
      - POSTGRESQL_USERNAME=my_user
      - POSTGRESQL_PASSWORD=my_password
      - POSTGRESQL_DATABASE=my_database
  postgresql-slave:
    image: 'bitnami/postgresql:latest'
    ports:
      - '5432'
    depends_on:
      - postgresql-master
    environment:
      - POSTGRESQL_REPLICATION_MODE=slave
      - POSTGRESQL_REPLICATION_USER=repl_user
      - POSTGRESQL_REPLICATION_PASSWORD=repl_password
      - POSTGRESQL_MASTER_HOST=postgresql-master
      - POSTGRESQL_PASSWORD=my_password
      - POSTGRESQL_MASTER_PORT_NUMBER=5432

volumes:
  postgresql_master_data:

Scale the number of slaves using:

$ docker-compose up --detach --scale postgresql-master=1 --scale postgresql-slave=3

The above command scales up the number of slaves to 3. You can scale down in the same way.

Note: You should not scale up/down the number of master nodes. Always have only one master node running.

⁠Synchronous commits

By default, the slaves instances are configued with asynchronous replication. In order to guarantee more data stability (at the cost of some performance), it is possible to set synchronous commits (i.e. a transaction commit will not return success to the client until it has been written in a set of replicas) using the following environment variables.

  • POSTGRESQL_SYNCHRONOUS_COMMIT_MODE: Establishes the type of synchronous commit. The available options are: on, remote_apply, remote_write, local and off. The default value is on. For more information, check the official PostgreSQL documentation⁠.
  • POSTGRESQL_NUM_SYNCHRONOUS_REPLICAS: Establishes the number of replicas that will enable synchronous replication. This number must not be above the number of slaves that you configure in the cluster.

With Docker Compose the master-slave replication with synchronous commits can be setup as follows:

version: '2'

services:
  postgresql-master:
    image: 'bitnami/postgresql:latest'
    ports:
      - '5432'
    volumes:
      - 'postgresql_master_data:/bitnami/postgresql'
    environment:
      - POSTGRESQL_REPLICATION_MODE=master
      - POSTGRESQL_REPLICATION_USER=repl_user
      - POSTGRESQL_REPLICATION_PASSWORD=repl_password
      - POSTGRESQL_USERNAME=my_user
      - POSTGRESQL_PASSWORD=my_password
      - POSTGRESQL_DATABASE=my_database
      - POSTGRESQL_SYNCHRONOUS_COMMIT_MODE=on
      - POSTGRESQL_NUM_SYNCHRONOUS_REPLICAS=1
    volumes:
      - '/path/to/postgresql-persistence:/bitnami/postgresql'
  postgresql-slave:
    image: 'bitnami/postgresql:latest'
    ports:
      - '5432'
    depends_on:
      - postgresql-master
    environment:
      - POSTGRESQL_REPLICATION_MODE=slave
      - POSTGRESQL_REPLICATION_USER=repl_user
      - POSTGRESQL_REPLICATION_PASSWORD=repl_password
      - POSTGRESQL_MASTER_HOST=postgresql-master
      - POSTGRESQL_MASTER_PORT_NUMBER=5432
  postgresql-slave2:
    image: 'bitnami/postgresql:latest'
    ports:
      - '5432'
    depends_on:
      - postgresql-master
    environment:
      - POSTGRESQL_REPLICATION_MODE=slave
      - POSTGRESQL_REPLICATION_USER=repl_user
      - POSTGRESQL_REPLICATION_PASSWORD=repl_password
      - POSTGRESQL_MASTER_HOST=postgresql-master
      - POSTGRESQL_MASTER_PORT_NUMBER=5432

In the example above, commits will need to be written to both the master and one of the slaves in order to be accepted. The other slave will continue using asynchronous replication. Check it with the following SQL query:

postgres=# select application_name as server, state,
postgres-#       sync_priority as priority, sync_state
postgres-#       from pg_stat_replication;
   server    |   state   | priority | sync_state
-------------|-----------|----------|------------
 walreceiver | streaming |        0 | sync
 walreceiver | streaming |        0 | async

Note: For more advanced setups, you can define different replication groups with the application_name parameter, by setting the POSTGRESQL_CLUSTER_APP_NAME environment variable.

⁠LDAP authentication

In order to use LDAP authentication you need to enable it setting the environment variable POSTGRESQL_ENABLE_LDAP to yes.

There are two ways of setting up the LDAP configuration:

  • By configuring POSTGRESQL_LDAP_URL, where you can configure all the associated parameters in the URL.
  • Setting up the parameters POSTGRESQL_LDAP_xxxx independently.

The LDAP related parameters are:

  • POSTGRESQL_LDAP_SERVER: IP addresses or names of the LDAP servers to connect to. Separated by spaces.
  • POSTGRESQL_LDAP_PORT: Port number on the LDAP server to connect to
  • POSTGRESQL_LDAP_SCHEME: Set to ldaps to use LDAPS. Default to none.
  • POSTGRESQL_LDAP_TLS: Set to 1 to use TLS encryption. Default to none.
  • POSTGRESQL_LDAP_PREFIX: String to prepend to the user name when forming the DN to bind. Default to none.
  • POSTGRESQL_LDAP_SUFFIX: String to append to the user name when forming the DN to bind. Default to none.
  • POSTGRESQL_LDAP_BASE_DN: Root DN to begin the search for the user in. Default to none.
  • POSTGRESQL_LDAP_BIND_DN: DN of user to bind to LDAP. Default to none.
  • POSTGRESQL_LDAP_BIND_PASSWORD: Password for the user to bind to LDAP. Default to none.
  • POSTGRESQL_LDAP_SEARCH_ATTR: Attribute to match against the user name in the search. Default to none.
  • POSTGRESQL_LDAP_SEARCH_FILTER: The search filter to use when doing search+bind authentication. Default to none.
  • POSTGRESQL_LDAP_URL: URL to connect to, in the format: ldap[s]://host[:port]/basedn[?[attribute][?[scope][?[filter]]]] .

For more information refer to Postgresql LDAP auth configuration documentation⁠.

⁠Securing PostgreSQL traffic

PostgreSQL supports the encryption of connections using the SSL/TLS protocol. Should you desire to enable this optional feature, you may use the following enviroment variables to configure the application:

  • POSTGRESQL_ENABLE_TLS: Whether to enable TLS for traffic or not. Defaults to no.
  • POSTGRESQL_TLS_CERT_FILE: File containing the certificate file for the TLS traffic. No defaults.
  • POSTGRESQL_TLS_KEY_FILE: File containing the key for certificate. No defaults.
  • POSTGRESQL_TLS_CA_FILE: File containing the CA of the certificate. If provided, PostgreSQL will authenticate TLS/SSL clients by requesting them a certificate (see ref⁠). No defaults.
  • POSTGRESQL_TLS_CRL_FILE: File containing a Certificate Revocation List. No defaults.
  • POSTGRESQL_TLS_PREFER_SERVER_CIPHERS: Whether to use the server's TLS cipher preferences rather than the client's. Defaults to yes.

When enabling TLS, PostgreSQL will support both standard and encrypted traffic by default, but prefer the latter. Below there are some examples on how to quickly set up TLS traffic:

  1. Using docker run

    $ docker run \
        -v /path/to/certs:/opt/bitnami/postgresql/certs \
        -e ALLOW_EMPTY_PASSWORD=yes \
        -e POSTGRESQL_ENABLE_TLS=yes \
        -e POSTGRESQL_TLS_CERT_FILE=/opt/bitnami/postgresql/certs/postgres.crt \
        -e POSTGRESQL_TLS_KEY_FILE=/opt/bitnami/postgresql/certs/postgres.key \
        bitnami/postgresql:latest
    
  2. Modifying the docker-compose.yml file present in this repository:

    services:
      postgresql:
      ...
        environment:
          ...
          - POSTGRESQL_ENABLE_TLS=yes
          - POSTGRESQL_TLS_CERT_FILE=/opt/bitnami/postgresql/certs/postgres.crt
          - POSTGRESQL_TLS_KEY_FILE=/opt/bitnami/postgresql/certs/postgres.key
        ...
        volumes:
          ...
          - /path/to/certs:/opt/bitnami/postgresql/certs
      ...
    

Alternatively, you may also provide this configuration in your custom⁠ configuration file.

⁠Configuration file

The image looks for postgresql.conf file in /opt/bitnami/postgresql/conf/. You can mount a volume at /bitnami/postgresql/conf/ and copy/edit the postgresql.conf file in the /path/to/postgresql-persistence/conf/. The default configurations will be populated to the conf/ directory if it's empty.

/path/to/postgresql-persistence/conf/
└── postgresql.conf

0 directories, 1 file

As PostgreSQL image is non-root, you need to set the proper permissions to the mounted directory in your host:

sudo chown 1001:1001 /path/to/postgresql-persistence/conf/
⁠Step 1: Run the PostgreSQL image

Run the PostgreSQL image, mounting a directory from your host.

$ docker run --name postgresql \
    -v /path/to/postgresql-persistence/conf/:/bitnami/postgresql/conf/ \
    bitnami/postgresql:latest

or using Docker Compose:

version: '2'

services:
  postgresql:
    image: 'bitnami/postgresql:latest'
    ports:
      - '5432:5432'
    volumes:
      - /path/to/postgresql-persistence/conf/:/bitnami/postgresql/conf/
⁠Step 2: Edit the configuration

Edit the configuration on your host using your favorite editor.

vi /path/to/postgresql-persistence/conf/postgresql.conf
⁠Step 3: Restart PostgreSQL

After changing the configuration, restart your PostgreSQL container for changes to take effect.

$ docker restart postgresql

or using Docker Compose:

$ docker-compose restart postgresql

Refer to the server configuration⁠ man

Tag summary

Content type

Image

Digest

Size

83 MB

Last updated

about 6 years ago

docker pull ashraj1/postgresql