{"id":13558564,"url":"https://github.com/sameersbn/docker-postgresql","last_synced_at":"2025-05-15T17:05:50.900Z","repository":{"id":15983112,"uuid":"18726133","full_name":"sameersbn/docker-postgresql","owner":"sameersbn","description":"Dockerfile to build a PostgreSQL container image which can be linked to other containers.","archived":false,"fork":false,"pushed_at":"2024-05-23T16:10:27.000Z","size":275,"stargazers_count":1067,"open_issues_count":54,"forks_count":474,"subscribers_count":39,"default_branch":"master","last_synced_at":"2025-05-15T12:58:46.008Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Shell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sameersbn.png","metadata":{"files":{"readme":"README.md","changelog":"Changelog.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-04-13T09:54:23.000Z","updated_at":"2025-04-28T03:15:57.000Z","dependencies_parsed_at":"2024-10-14T15:21:26.788Z","dependency_job_id":null,"html_url":"https://github.com/sameersbn/docker-postgresql","commit_stats":null,"previous_names":[],"tags_count":46,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sameersbn%2Fdocker-postgresql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sameersbn%2Fdocker-postgresql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sameersbn%2Fdocker-postgresql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sameersbn%2Fdocker-postgresql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sameersbn","download_url":"https://codeload.github.com/sameersbn/docker-postgresql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254384988,"owners_count":22062422,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-01T12:05:01.805Z","updated_at":"2025-05-15T17:05:45.892Z","avatar_url":"https://github.com/sameersbn.png","language":"Shell","funding_links":[],"categories":["Shell","others"],"sub_categories":[],"readme":"# sameersbn/postgresql:15-20230628\n\n- [Introduction](#introduction)\n  - [Contributing](#contributing)\n  - [Issues](#issues)\n- [Getting started](#getting-started)\n  - [Installation](#installation)\n  - [Quickstart](#quickstart)\n  - [Persistence](#persistence)\n  - [Trusting local connections](#trusting-local-connections)\n  - [Setting `postgres` user password](#setting-postgres-user-password)\n  - [Creating database user](#creating-database-user)\n  - [Creating databases](#creating-databases)\n  - [Granting user access to a database](#granting-user-access-to-a-database)\n  - [Enabling extensions](#enabling-extensions)\n  - [Creating replication user](#creating-replication-user)\n  - [Setting up a replication cluster](#setting-up-a-replication-cluster)\n  - [Creating a snapshot](#creating-a-snapshot)\n  - [Creating a backup](#creating-a-backup)\n  - [Command-line arguments](#command-line-arguments)\n  - [Logs](#logs)\n  - [UID/GID mapping](#uidgid-mapping)\n- [Maintenance](#maintenance)\n  - [Upgrading](#upgrading)\n  - [Shell Access](#shell-access)\n\n# Introduction\n\n`Dockerfile` to create a [Docker](https://www.docker.com/) container image for [PostgreSQL](http://postgresql.org/).\n\nPostgreSQL is an object-relational database management system (ORDBMS) with an emphasis on extensibility and standards-compliance [[source](https://en.wikipedia.org/wiki/PostgreSQL)].\n\n## Contributing\n\nIf you find this image useful here's how you can help:\n\n- Send a pull request with your awesome features and bug fixes\n- Help users resolve their [issues](../../issues?q=is%3Aopen+is%3Aissue).\n- Support the development of this image with a [donation](http://www.damagehead.com/donate/)\n\n## Issues\n\nBefore reporting your issue please try updating Docker to the latest version and check if it resolves the issue. Refer to the Docker [installation guide](https://docs.docker.com/installation) for instructions.\n\nSELinux users should try disabling SELinux using the command `setenforce 0` to see if it resolves the issue.\n\nIf the above recommendations do not help then [report your issue](../../issues/new) along with the following information:\n\n- Output of the `docker vers6` and `docker info` commands\n- The `docker run` command or `docker-compose.yml` used to start the image. Mask out the sensitive bits.\n- Please state if you are using [Boot2Docker](http://www.boot2docker.io), [VirtualBox](https://www.virtualbox.org), etc.\n\n# Getting started\n\n## Installation\n\nAutomated builds of the image are available on [Dockerhub](https://hub.docker.com/r/sameersbn/postgresql) and is the recommended method of installation.\n\n\u003e **Note**: Builds are also available on [Quay.io](https://quay.io/repository/sameersbn/postgresql)\n\n```bash\ndocker pull sameersbn/postgresql:15-20230628\n```\n\nAlternatively you can build the image yourself.\n\n```bash\ndocker build -t sameersbn/postgresql github.com/sameersbn/docker-postgresql\n```\n\n## Quickstart\n\nStart PostgreSQL using:\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --publish 5432:5432 \\\n  --volume postgresql:/var/lib/postgresql \\\n  sameersbn/postgresql:15-20230628\n```\n\nLogin to the PostgreSQL server using:\n\n```bash\ndocker exec -it postgresql sudo -u postgres psql\n```\n\n*Alternatively, you can use the sample [docker-compose.yml](docker-compose.yml) file to start the container using [Docker Compose](https://docs.docker.com/compose/)*\n\n## Persistence\n\nFor PostgreSQL to preserve its state across container shutdown and startup you should mount a volume at `/var/lib/postgresql`. If you don't like the default volume destination then you can change it\n\n\u003e *The [Quickstart](#quickstart) command already mounts a volume for persistence.*\n\nSELinux users should update the security context of the host mountpoint so that it plays nicely with Docker:\n\n```bash\nmkdir -p /srv/docker/postgresql\nchcon -Rt svirt_sandbox_file_t /srv/docker/postgresql\n```\n\n## Trusting local connections\n\nBy default connections to the PostgreSQL server need to authenticated using a password. If desired you can trust connections from the local network using the `PG_TRUST_LOCALNET` variable.\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'PG_TRUST_LOCALNET=true' \\\n  sameersbn/postgresql:15-20230628\n```\n\n\u003e **Note**\n\u003e\n\u003e The local network here is network to which the container is attached. This has different meanings depending on the `--net` parameter specified while starting the container. In the default configuration, this parameter would trust connections from other containers on the `docker0` bridge.\n\n## Setting `postgres` user password\n\nBy default the `postgres` user is not assigned a password and as a result you can only login to the PostgreSQL server locally. If you wish to login remotely to the PostgreSQL server as the `postgres` user, you will need to assign a password for the user using the `PG_PASSWORD` variable.\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'PG_PASSWORD=passw0rd' \\\n  sameersbn/postgresql:15-20230628\n```\n\n\n\u003e **Note**\n\u003e\n\u003e - When [persistence](#persistence) is in use, `PG_PASSWORD` is effective on the first run.\n\u003e - This feature is only available in the `latest` and versions \u003e `9.4-10`\n\n## Creating database user\n\nA new PostgreSQL database user can be created by specifying the `DB_USER` and `DB_PASS` variables while starting the container.\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'DB_USER=dbuser' --env 'DB_PASS=dbuserpass' \\\n  sameersbn/postgresql:15-20230628\n```\n\n\u003e **Notes**\n\u003e\n\u003e - The created user can login remotely\n\u003e - The container will error out if a password is not specified for the user\n\u003e - No changes will be made if the user already exists\n\u003e - Only a single user can be created at each launch\n\n## Creating databases\n\nA new PostgreSQL database can be created by specifying the `DB_NAME` variable while starting the container.\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'DB_NAME=dbname' \\\n  sameersbn/postgresql:15-20230628\n```\n\nBy default databases are created by copying the standard system database named `template1`. You can specify a different template for your database using the `DB_TEMPLATE` parameter. Refer to [Template Databases](http://www.postgresql.org/docs/9.4/static/manage-ag-templatedbs.html) for further information.\n\nAdditionally, more than one database can be created by specifying a comma separated list of database names in `DB_NAME`. For example, the following command creates two new databases named `dbname1` and `dbname2`.\n\n*This feature is only available in releases greater than `9.1-1`*\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'DB_NAME=dbname1,dbname2' \\\n  sameersbn/postgresql:15-20230628\n```\n\n## Granting user access to a database\n\nIf the `DB_USER` and `DB_PASS` variables are specified along with the `DB_NAME` variable, then the user specified in `DB_USER` will be granted access to all the databases listed in `DB_NAME`. Note that if the user and/or databases do not exist, they will be created.\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'DB_USER=dbuser' --env 'DB_PASS=dbuserpass' \\\n  --env 'DB_NAME=dbname1,dbname2' \\\n  sameersbn/postgresql:15-20230628\n```\n\nIn the above example `dbuser` with be granted access to both the `dbname1` and `dbname2` databases.\n\n# Enabling extensions\n\nThe image also packages the [postgres contrib module](http://www.postgresql.org/docs/9.4/static/contrib.html). A comma separated list of modules can be specified using the `DB_EXTENSION` parameter.\n\n```bash\ndocker run --name postgresql -itd \\\n  --env 'DB_NAME=db1,db2' --env 'DB_EXTENSION=unaccent,pg_trgm' \\\n  sameersbn/postgresql:15-20230628\n```\n\nThe above command enables the `unaccent` and `pg_trgm` modules on the databases listed in `DB_NAME`, namely `db1` and `db2`.\n\n\u003e **NOTE**:\n\u003e\n\u003e This option deprecates the `DB_UNACCENT` parameter.\n\n## Creating replication user\n\nSimilar to the creation of a database user, a new PostgreSQL replication user can be created by specifying the `REPLICATION_USER` and `REPLICATION_PASS` variables while starting the container.\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'REPLICATION_USER=repluser' --env 'REPLICATION_PASS=repluserpass' \\\n  sameersbn/postgresql:15-20230628\n```\n\n\u003e **Notes**\n\u003e\n\u003e - The created user can login remotely\n\u003e - The container will error out if a password is not specified for the user\n\u003e - No changes will be made if the user already exists\n\u003e - Only a single user can be created at each launch\n\n*It is a good idea to create a replication user even if you are not going to use it as it will allow you to setup slave nodes and/or generate snapshots and backups when the need arises.*\n\n## Setting up a replication cluster\n\nWhen the container is started, it is by default configured to act as a master node in a replication cluster. This means that you can scale your PostgreSQL database backend when the need arises without incurring any downtime. However do note that a replication user must exist on the master node for this to work.\n\nBegin by creating the master node of our cluster:\n\n```bash\ndocker run --name postgresql-master -itd --restart always \\\n  --env 'DB_USER=dbuser' --env 'DB_PASS=dbuserpass' --env 'DB_NAME=dbname' \\\n  --env 'REPLICATION_USER=repluser' --env 'REPLICATION_PASS=repluserpass' \\\n  sameersbn/postgresql:15-20230628\n```\n\nNotice that no additional arguments are specified while starting the master node of the cluster.\n\nTo create a replication slave the `REPLICATION_MODE` variable should be set to `slave` and additionally the `REPLICATION_HOST`, `REPLICATION_PORT`, `REPLICATION_SSLMODE`, `REPLICATION_USER` and `REPLICATION_PASS` variables should be specified.\n\nCreate a slave node:\n\n```bash\ndocker run --name postgresql-slave01 -itd --restart always \\\n  --link postgresql-master:master \\\n  --env 'REPLICATION_MODE=slave' --env 'REPLICATION_SSLMODE=prefer' \\\n  --env 'REPLICATION_HOST=master' --env 'REPLICATION_PORT=5432'  \\\n  --env 'REPLICATION_USER=repluser' --env 'REPLICATION_PASS=repluserpass' \\\n  sameersbn/postgresql:15-20230628\n```\n\n*In the above command, we used docker links so that we can address the master node using the `master` alias in `REPLICATION_HOST`.*\n\n\u003e **Note**\n\u003e\n\u003e - The default value of `REPLICATION_PORT` is `5432`\n\u003e - The default value of `REPLICATION_SSLMODE` is `prefer`\n\u003e - The value of `REPLICATION_USER` and `REPLICATION_PASS` should be the same as the ones specified on the master node.\n\u003e - With [persistence](#persistence) in use, if the container is stopped and started, for the container continue to function as a slave you need to ensure that `REPLICATION_MODE=slave` is defined in the containers environment. In the absense of which the slave configuration will be turned off and the node will allow writing to it while having the last synced data from the master.\n\nAnd just like that with minimal effort you have a PostgreSQL replication cluster setup. You can create additional slaves to scale the cluster horizontally.\n\nHere are some important notes about a PostgreSQL replication cluster:\n\n - Writes can only occur on the master\n - Slaves are read-only\n - For best performance, limit the reads to the slave nodes\n\n## Creating a snapshot\n\nSimilar to a creating replication slave node, you can create a snapshot of the master by specifying `REPLICATION_MODE=snapshot`.\n\nOnce the master node is created as specified in [Setting up a replication cluster](#setting-up-a-replication-cluster), you can create a snapshot using:\n\n```bash\ndocker run --name postgresql-snapshot -itd --restart always \\\n  --link postgresql-master:master \\\n  --env 'REPLICATION_MODE=snapshot' --env 'REPLICATION_SSLMODE=prefer' \\\n  --env 'REPLICATION_HOST=master' --env 'REPLICATION_PORT=5432'  \\\n  --env 'REPLICATION_USER=repluser' --env 'REPLICATION_PASS=repluserpass' \\\n  sameersbn/postgresql:15-20230628\n```\n\nThe difference between a slave and a snapshot is that a slave is read-only and updated whenever the master data is updated (streaming replication), while a snapshot is read-write and is not updated after the initial snapshot of the data from the master.\n\nThis is useful for developers to quickly snapshot the current state of a live database and use it for development/debugging purposes without altering the database on the live instance.\n\n## Creating a backup\n\nJust as the case of setting up a slave node or generating a snapshot, you can also create a backup of the data on the master by specifying `REPLICATION_MODE=backup`.\n\n\u003e The backups are generated with [pg_basebackup](http://www.postgresql.org/docs/9.4/static/app-pgbasebackup.html) using the replication protocol.\n\nOnce the master node is created as specified in [Setting up a replication cluster](#setting-up-a-replication-cluster), you can create a point-in-time backup using:\n\n```bash\ndocker run --name postgresql-backup -it --rm \\\n  --link postgresql-master:master \\\n  --env 'REPLICATION_MODE=backup' --env 'REPLICATION_SSLMODE=prefer' \\\n  --env 'REPLICATION_HOST=master' --env 'REPLICATION_PORT=5432'  \\\n  --env 'REPLICATION_USER=repluser' --env 'REPLICATION_PASS=repluserpass' \\\n  --volume /srv/docker/backups/postgresql.$(date +%Y%m%d%H%M%S):/var/lib/postgresql \\\n  sameersbn/postgresql:15-20230628\n```\n\nOnce the backup is generated, the container will exit and the backup of the master data will be available at `/srv/docker/backups/postgresql.XXXXXXXXXXXX/`. Restoring the backup involves starting a container with the data in `/srv/docker/backups/postgresql.XXXXXXXXXXXX`.\n\n## Command-line arguments\n\nYou can customize the launch command of PostgreSQL server by specifying arguments for `postgres` on the `docker run` command. For example the following command enables connection logging:\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  sameersbn/postgresql:15-20230628 -c log_connections=on\n```\n\nPlease refer to the documentation of [postgres](http://www.postgresql.org/docs/9.4/static/app-postgres.html) for the complete list of available options.\n\n## Logs\n\nBy default the PostgreSQL server logs are sent to the standard output. Using the [Command-line arguments](#command-line-arguments) feature you can configure the PostgreSQL server to send the log output to a file using the `-c logging_collector=on` argument:\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  sameersbn/postgresql:15-20230628 -c logging_collector=on\n```\n\nTo access the PostgreSQL logs you can use `docker exec`. For example:\n\n```bash\ndocker exec -it postgresql tail -f /var/log/postgresql/postgresql-9.4-main.log\n```\n\n# UID/GID mapping\n\nThe files and processes created by the container are owned by the `postgres` user that is internal to the container. In the absense of user namespace in docker the UID and GID of the containers `postgres` user may have different meaning on the host.\n\nFor example, a user on the host with the same UID and/or GID as the `postgres` user of the container will be able to access the data in the persistent volumes mounted from the host as well as be able to KILL the `postgres` server process started by the container.\n\nTo circumvent this issue you can specify the UID and GID for the `postgres` user of the container using the `USERMAP_UID` and `USERMAP_GID` variables respectively.\n\nFor example, if you want to assign the `postgres` user of the container the UID and GID `999`:\n\n```bash\ndocker run --name postgresql -itd --restart always \\\n  --env 'USERMAP_UID=999' --env 'USERMAP_GID=999' \\\n  sameersbn/postgresql:15-20230628\n```\n\n# Maintenance\n\n## Upgrading\n\nTo upgrade to newer releases:\n\n  1. Download the updated Docker image:\n\n  ```bash\n  docker pull sameersbn/postgresql:15-20230628\n  ```\n\n  2. Stop the currently running image:\n\n  ```bash\n  docker stop postgresql\n  ```\n\n  3. Remove the stopped container\n\n  ```bash\n  docker rm -v postgresql\n  ```\n\n  4. Start the updated image\n\n  ```bash\n  docker run --name postgresql -itd \\\n    [OPTIONS] \\\n    sameersbn/postgresql:15-20230628\n  ```\n\n## Shell Access\n\nFor 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 by starting `bash` using `docker exec`:\n\n```bash\ndocker exec -it postgresql bash\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsameersbn%2Fdocker-postgresql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsameersbn%2Fdocker-postgresql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsameersbn%2Fdocker-postgresql/lists"}