github.com/guilhermebr/docker@v1.4.2-0.20150428121140-67da055cebca/docs/sources/examples/postgresql_service.md (about) 1 page_title: Dockerizing PostgreSQL 2 page_description: Running and installing a PostgreSQL service 3 page_keywords: docker, example, package installation, postgresql 4 5 # Dockerizing PostgreSQL 6 7 > **Note**: 8 > - **If you don't like sudo** then see [*Giving non-root 9 > access*](/installation/binaries/#giving-non-root-access) 10 11 ## Installing PostgreSQL on Docker 12 13 Assuming there is no Docker image that suits your needs on the [Docker 14 Hub](http://hub.docker.com), you can create one yourself. 15 16 Start by creating a new `Dockerfile`: 17 18 > **Note**: 19 > This PostgreSQL setup is for development-only purposes. Refer to the 20 > PostgreSQL documentation to fine-tune these settings so that it is 21 > suitably secure. 22 23 # 24 # example Dockerfile for http://docs.docker.com/examples/postgresql_service/ 25 # 26 27 FROM ubuntu 28 MAINTAINER SvenDowideit@docker.com 29 30 # Add the PostgreSQL PGP key to verify their Debian packages. 31 # It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc 32 RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 33 34 # Add PostgreSQL's repository. It contains the most recent stable release 35 # of PostgreSQL, ``9.3``. 36 RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list 37 38 # Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3 39 # There are some warnings (in red) that show up during the build. You can hide 40 # them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive 41 RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 42 43 # Note: The official Debian and Ubuntu images automatically ``apt-get clean`` 44 # after each ``apt-get`` 45 46 # Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed`` 47 USER postgres 48 49 # Create a PostgreSQL role named ``docker`` with ``docker`` as the password and 50 # then create a database `docker` owned by the ``docker`` role. 51 # Note: here we use ``&&\`` to run commands one after the other - the ``\`` 52 # allows the RUN command to span multiple lines. 53 RUN /etc/init.d/postgresql start &&\ 54 psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\ 55 createdb -O docker docker 56 57 # Adjust PostgreSQL configuration so that remote connections to the 58 # database are possible. 59 RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf 60 61 # And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf`` 62 RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf 63 64 # Expose the PostgreSQL port 65 EXPOSE 5432 66 67 # Add VOLUMEs to allow backup of config, logs and databases 68 VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"] 69 70 # Set the default command to run when starting the container 71 CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"] 72 73 Build an image from the Dockerfile assign it a name. 74 75 $ docker build -t eg_postgresql . 76 77 And run the PostgreSQL server container (in the foreground): 78 79 $ docker run --rm -P --name pg_test eg_postgresql 80 81 There are 2 ways to connect to the PostgreSQL server. We can use [*Link 82 Containers*](/userguide/dockerlinks), or we can access it from our host 83 (or the network). 84 85 > **Note**: 86 > The `--rm` removes the container and its image when 87 > the container exits successfully. 88 89 ### Using container linking 90 91 Containers can be linked to another container's ports directly using 92 `-link remote_name:local_alias` in the client's 93 `docker run`. This will set a number of environment 94 variables that can then be used to connect: 95 96 $ docker run --rm -t -i --link pg_test:pg eg_postgresql bash 97 98 postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password 99 100 ### Connecting from your host system 101 102 Assuming you have the postgresql-client installed, you can use the 103 host-mapped port to test as well. You need to use `docker ps` 104 to find out what local host port the container is mapped to 105 first: 106 107 $ docker ps 108 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 109 5e24362f27f6 eg_postgresql:latest /usr/lib/postgresql/ About an hour ago Up About an hour 0.0.0.0:49153->5432/tcp pg_test 110 $ psql -h localhost -p 49153 -d docker -U docker --password 111 112 ### Testing the database 113 114 Once you have authenticated and have a `docker =#` 115 prompt, you can create a table and populate it. 116 117 psql (9.3.1) 118 Type "help" for help. 119 120 $ docker=# CREATE TABLE cities ( 121 docker(# name varchar(80), 122 docker(# location point 123 docker(# ); 124 CREATE TABLE 125 $ docker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)'); 126 INSERT 0 1 127 $ docker=# select * from cities; 128 name | location 129 ---------------+----------- 130 San Francisco | (-194,53) 131 (1 row) 132 133 ### Using the container volumes 134 135 You can use the defined volumes to inspect the PostgreSQL log files and 136 to backup your configuration and data: 137 138 $ docker run --rm --volumes-from pg_test -t -i busybox sh 139 140 / # ls 141 bin etc lib linuxrc mnt proc run sys usr 142 dev home lib64 media opt root sbin tmp var 143 / # ls /etc/postgresql/9.3/main/ 144 environment pg_hba.conf postgresql.conf 145 pg_ctl.conf pg_ident.conf start.conf 146 /tmp # ls /var/log 147 ldconfig postgresql