postgresql_service.rst 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 Pitti's PostgreSQL repository. It contains the most recent stable release
  30. of PostgreSQL i.e. ``9.2``.
  31. .. code-block:: bash
  32. add-apt-repository ppa:pitti/postgresql
  33. apt-get update
  34. Finally, install PostgreSQL 9.2
  35. .. code-block:: bash
  36. apt-get -y install postgresql-9.2 postgresql-client-9.2 postgresql-contrib-9.2
  37. Now, create a PostgreSQL superuser role that can create databases and
  38. other roles. Following Vagrant's convention the role will be named
  39. ``docker`` with ``docker`` password assigned to it.
  40. .. code-block:: bash
  41. su postgres -c "createuser -P -d -r -s docker"
  42. Create a test database also named ``docker`` owned by previously created ``docker``
  43. role.
  44. .. code-block:: bash
  45. su postgres -c "createdb -O docker docker"
  46. Adjust PostgreSQL configuration so that remote connections to the
  47. database are possible. Make sure that inside
  48. ``/etc/postgresql/9.2/main/pg_hba.conf`` you have following line (you will need
  49. to install an editor, e.g. ``apt-get install vim``):
  50. .. code-block:: bash
  51. host all all 0.0.0.0/0 md5
  52. Additionaly, inside ``/etc/postgresql/9.2/main/postgresql.conf``
  53. uncomment ``listen_addresses`` so it is as follows:
  54. .. code-block:: bash
  55. listen_addresses='*'
  56. .. note::
  57. This PostgreSQL setup is for development only purposes. Refer
  58. to PostgreSQL documentation how to fine-tune these settings so that it
  59. is enough secure.
  60. Exit.
  61. .. code-block:: bash
  62. exit
  63. Create an image and assign it a name. ``<container_id>`` is in the
  64. Bash prompt; you can also locate it using ``docker ps -a``.
  65. .. code-block:: bash
  66. sudo docker commit <container_id> <your username>/postgresql
  67. Finally, run PostgreSQL server via ``docker``.
  68. .. code-block:: bash
  69. CONTAINER=$(sudo docker run -d -p 5432 \
  70. -t <your username>/postgresql \
  71. /bin/su postgres -c '/usr/lib/postgresql/9.2/bin/postgres \
  72. -D /var/lib/postgresql/9.2/main \
  73. -c config_file=/etc/postgresql/9.2/main/postgresql.conf')
  74. Connect the PostgreSQL server using ``psql`` (You will need postgres installed
  75. on the machine. For ubuntu, use something like
  76. ``sudo apt-get install postgresql``).
  77. .. code-block:: bash
  78. CONTAINER_IP=$(sudo docker inspect $CONTAINER | grep IPAddress | awk '{ print $2 }' | tr -d ',"')
  79. psql -h $CONTAINER_IP -p 5432 -d docker -U docker -W
  80. As before, create roles or databases if needed.
  81. .. code-block:: bash
  82. psql (9.2.4)
  83. Type "help" for help.
  84. docker=# CREATE DATABASE foo OWNER=docker;
  85. CREATE DATABASE
  86. Additionally, publish your newly created image on Docker Index.
  87. .. code-block:: bash
  88. sudo docker login
  89. Username: <your username>
  90. [...]
  91. .. code-block:: bash
  92. sudo docker push <your username>/postgresql
  93. PostgreSQL service auto-launch
  94. ------------------------------
  95. Running our image seems complicated. We have to specify the whole command with
  96. ``docker run``. Let's simplify it so the service starts automatically when the
  97. container starts.
  98. .. code-block:: bash
  99. sudo docker commit -run='{"Cmd": \
  100. ["/bin/su", "postgres", "-c", "/usr/lib/postgresql/9.2/bin/postgres -D \
  101. /var/lib/postgresql/9.2/main -c \
  102. config_file=/etc/postgresql/9.2/main/postgresql.conf"], "PortSpecs": ["5432"]}' \
  103. <container_id> <your username>/postgresql
  104. From now on, just type ``docker run <your username>/postgresql`` and
  105. PostgreSQL should automatically start.