postgresql_service.rst 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. :title: PostgreSQL service How-To
  2. :description: Running and installing a PostgreSQL service
  3. :keywords: docker, example, package installation, postgresql
  4. .. _postgresql_service:
  5. PostgreSQL Service
  6. ==================
  7. .. note::
  8. A shorter version of `this blog post`_.
  9. .. note::
  10. As of version 0.5.2, Docker requires root privileges to run.
  11. You have to either manually adjust your system configuration (permissions on
  12. /var/run/docker.sock or sudo config), or prefix `docker` with `sudo`. Check
  13. `this thread`_ for details.
  14. .. _this blog post: http://zaiste.net/2013/08/docker_postgresql_how_to/
  15. .. _this thread: https://groups.google.com/forum/?fromgroups#!topic/docker-club/P3xDLqmLp0E
  16. Installing PostgreSQL on Docker
  17. -------------------------------
  18. For clarity I won't be showing command output.
  19. Run an interactive shell in Docker container.
  20. .. code-block:: bash
  21. sudo docker run -i -t ubuntu /bin/bash
  22. Update its dependencies.
  23. .. code-block:: bash
  24. apt-get update
  25. Install ``python-software-properties``.
  26. .. code-block:: bash
  27. apt-get -y install python-software-properties
  28. apt-get -y install software-properties-common
  29. Add PostgreSQL's repository. It contains the most recent stable release
  30. of PostgreSQL i.e. ``9.3``.
  31. .. code-block:: bash
  32. apt-get -y install wget
  33. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
  34. echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
  35. apt-get update
  36. Finally, install PostgreSQL 9.3
  37. .. code-block:: bash
  38. apt-get -y install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
  39. Now, create a PostgreSQL superuser role that can create databases and
  40. other roles. Following Vagrant's convention the role will be named
  41. ``docker`` with ``docker`` password assigned to it.
  42. .. code-block:: bash
  43. su postgres -c "createuser -P -d -r -s docker"
  44. Create a test database also named ``docker`` owned by previously created ``docker``
  45. role.
  46. .. code-block:: bash
  47. su postgres -c "createdb -O docker docker"
  48. Adjust PostgreSQL configuration so that remote connections to the
  49. database are possible. Make sure that inside
  50. ``/etc/postgresql/9.3/main/pg_hba.conf`` you have following line (you will need
  51. to install an editor, e.g. ``apt-get install vim``):
  52. .. code-block:: bash
  53. host all all 0.0.0.0/0 md5
  54. Additionaly, inside ``/etc/postgresql/9.3/main/postgresql.conf``
  55. uncomment ``listen_addresses`` so it is as follows:
  56. .. code-block:: bash
  57. listen_addresses='*'
  58. .. note::
  59. This PostgreSQL setup is for development only purposes. Refer
  60. to PostgreSQL documentation how to fine-tune these settings so that it
  61. is enough secure.
  62. Exit.
  63. .. code-block:: bash
  64. exit
  65. Create an image and assign it a name. ``<container_id>`` is in the
  66. Bash prompt; you can also locate it using ``docker ps -a``.
  67. .. code-block:: bash
  68. sudo docker commit <container_id> <your username>/postgresql
  69. Finally, run PostgreSQL server via ``docker``.
  70. .. code-block:: bash
  71. CONTAINER=$(sudo docker run -d -p 5432 \
  72. -t <your username>/postgresql \
  73. /bin/su postgres -c '/usr/lib/postgresql/9.3/bin/postgres \
  74. -D /var/lib/postgresql/9.3/main \
  75. -c config_file=/etc/postgresql/9.3/main/postgresql.conf')
  76. Connect the PostgreSQL server using ``psql`` (You will need postgres installed
  77. on the machine. For ubuntu, use something like
  78. ``sudo apt-get install postgresql``).
  79. .. code-block:: bash
  80. CONTAINER_IP=$(sudo docker inspect -format='{{.NetworkSettings.IPAddress}}' $CONTAINER)
  81. psql -h $CONTAINER_IP -p 5432 -d docker -U docker -W
  82. As before, create roles or databases if needed.
  83. .. code-block:: bash
  84. psql (9.3.1)
  85. Type "help" for help.
  86. docker=# CREATE DATABASE foo OWNER=docker;
  87. CREATE DATABASE
  88. Additionally, publish your newly created image on Docker Index.
  89. .. code-block:: bash
  90. sudo docker login
  91. Username: <your username>
  92. [...]
  93. .. code-block:: bash
  94. sudo docker push <your username>/postgresql
  95. PostgreSQL service auto-launch
  96. ------------------------------
  97. Running our image seems complicated. We have to specify the whole command with
  98. ``docker run``. Let's simplify it so the service starts automatically when the
  99. container starts.
  100. .. code-block:: bash
  101. sudo docker commit -run='{"Cmd": \
  102. ["/bin/su", "postgres", "-c", "/usr/lib/postgresql/9.3/bin/postgres -D \
  103. /var/lib/postgresql/9.3/main -c \
  104. config_file=/etc/postgresql/9.3/main/postgresql.conf"], "PortSpecs": ["5432"]}' \
  105. <container_id> <your username>/postgresql
  106. From now on, just type ``docker run <your username>/postgresql`` and
  107. PostgreSQL should automatically start.