postgresql_service.rst 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 commands 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 install python-software-properties
  28. apt-get 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:
  49. .. code-block:: bash
  50. host all all 0.0.0.0/0 md5
  51. Additionaly, inside ``/etc/postgresql/9.2/main/postgresql.conf``
  52. uncomment ``listen_addresses`` so it is as follows:
  53. .. code-block:: bash
  54. listen_addresses='*'
  55. *Note:* this PostgreSQL setup is for development only purposes. Refer
  56. to PostgreSQL documentation how to fine-tune these settings so that it
  57. is enough secure.
  58. Create an image and assign it a name. ``<container_id>`` is in the
  59. Bash prompt; you can also locate it using ``docker ps -a``.
  60. .. code-block:: bash
  61. docker commit <container_id> <your username>/postgresql
  62. Finally, run PostgreSQL server via ``docker``.
  63. .. code-block:: bash
  64. CONTAINER=$(sudo docker run -d -p 5432 \
  65. -t <your username>/postgresql \
  66. /bin/su postgres -c '/usr/lib/postgresql/9.2/bin/postgres \
  67. -D /var/lib/postgresql/9.2/main \
  68. -c config_file=/etc/postgresql/9.2/main/postgresql.conf')
  69. Connect the PostgreSQL server using ``psql``.
  70. .. code-block:: bash
  71. CONTAINER_IP=$(sudo docker inspect $CONTAINER | grep IPAddress | awk '{ print $2 }' | tr -d ',"')
  72. psql -h $CONTAINER_IP -p 5432 -d docker -U docker -W
  73. As before, create roles or databases if needed.
  74. .. code-block:: bash
  75. psql (9.2.4)
  76. Type "help" for help.
  77. docker=# CREATE DATABASE foo OWNER=docker;
  78. CREATE DATABASE
  79. Additionally, publish there your newly created image on Docker Index.
  80. .. code-block:: bash
  81. sudo docker login
  82. Username: <your username>
  83. [...]
  84. .. code-block:: bash
  85. sudo docker push <your username>/postgresql
  86. PostgreSQL service auto-launch
  87. ------------------------------
  88. Running our image seems complicated. We have to specify the whole command with
  89. ``docker run``. Let's simplify it so the service starts automatically when the
  90. container starts.
  91. .. code-block:: bash
  92. sudo docker commit <container_id> <your username> postgresql -run='{"Cmd": \
  93. ["/bin/su", "postgres", "-c", "/usr/lib/postgresql/9.2/bin/postgres -D \
  94. /var/lib/postgresql/9.2/main -c \
  95. config_file=/etc/postgresql/9.2/main/postgresql.conf"], "PortSpecs": ["5432"]}'
  96. From now on, just type ``docker run <your username>/postgresql`` and
  97. PostgreSQL should automatically start.