Table of Contents
- Reporting Issues
- Quick Start
- Creating User and Database at Launch
- Shell Access
Dockerfile to build a PostgreSQL container image which can be linked to other containers.
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 they may encounter
- Send me a tip via Bitcoin or using Gratipay
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 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.
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 page.
In your issue report please make sure you provide the following information:
- The host ditribution and release version.
- Output of the
- Output of the
docker runcommand you used to run the image (mask out the sensitive bits).
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.
docker pull sameersbn/postgresql:9.4
Alternately you can build the image yourself.
git clone https://github.com/sameersbn/docker-postgresql.git cd docker-postgresql docker build -t="$USER/postgresql" .
Run the postgresql image
docker run --name postgresql -d sameersbn/postgresql:9.4
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.
docker run -it --rm --volumes-from=postgresql \ sameersbn/postgresql:9.4 sudo -u postgres -H psql
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_PASS variables. The following command will create a new user dbuser with the password dbpass.
docker run --name postgresql -d \ -e 'DB_USER=dbuser' -e 'DB_PASS=dbpass' \ sameersbn/postgresql:9.4
- 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
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)
docker run --name postgresql -d \ -e 'DB_NAME=dbname1,dbname2' sameersbn/postgresql:latest
DB_PASS variables are also specified while creating the database, then the user is granted access to the database(s).
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.
PSQL_TRUST_LOCALNET environment variable can be used to configure postgres to trust connections on the same network. This is handy for other containers to connect without authentication. To enable this behavior, set
docker run --name postgresql -d \ -e 'PSQL_TRUST_LOCALNET=true' \ sameersbn/postgresql:9.4
This has the effect of adding the following to the
host all all samenet trust
For data persistence a volume should be mounted at
SELinux users are also required to change the security context of the mount point so that it plays nicely with selinux.
mkdir -p /opt/postgresql/data sudo chcon -Rt svirt_sandbox_file_t /opt/postgresql/data
The updated run command looks like this.
docker run --name postgresql -d \ -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.
Enable Unaccent (Search plain text with accent)
Unaccent is a text search dictionary that removes accents (diacritic signs) from lexemes. It's a filtering dictionary, which means its output is always passed to the next dictionary (if any), unlike the normal behavior of dictionaries. This allows accent-insensitive processing for full text search.
By default unaccent is configure to
docker run --name postgresql -d \ -e 'DB_UNACCENT=true' \ sameersbn/postgresql:9.4
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.
docker exec -it postgresql bash
If you are using an older version of docker, you can use the nsenter 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.
nsenter execute the following command on your host,
docker run --rm -v /usr/local/bin:/target jpetazzo/nsenter
Now you can access the container shell using the command
sudo docker-enter postgresql
For more information refer https://github.com/jpetazzo/nsenter
To upgrade to newer releases, simply follow this 3 step upgrade procedure.
- Step 1: Stop the currently running image
docker stop postgresql
- Step 2: Update the docker image.
docker pull sameersbn/postgresql:9.4
- Step 3: Start the image
docker run --name postgresql -d [OPTIONS] sameersbn/postgresql:9.4