postgresql_service.rst 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. .. include:: example_header.inc
  8. .. note::
  9. A shorter version of `this blog post`_.
  10. .. _this blog post: http://zaiste.net/2013/08/docker_postgresql_how_to/
  11. Installing PostgreSQL on Docker
  12. -------------------------------
  13. Run an interactive shell in a Docker container.
  14. .. code-block:: bash
  15. sudo docker run -i -t ubuntu /bin/bash
  16. Update its dependencies.
  17. .. code-block:: bash
  18. apt-get update
  19. Install ``python-software-properties``, ``software-properties-common``, ``wget`` and ``vim``.
  20. .. code-block:: bash
  21. apt-get -y install python-software-properties software-properties-common wget vim
  22. Add PostgreSQL's repository. It contains the most recent stable release
  23. of PostgreSQL, ``9.3``.
  24. .. code-block:: bash
  25. wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
  26. echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
  27. apt-get update
  28. Finally, install PostgreSQL 9.3
  29. .. code-block:: bash
  30. apt-get -y install postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
  31. Now, create a PostgreSQL superuser role that can create databases and
  32. other roles. Following Vagrant's convention the role will be named
  33. ``docker`` with ``docker`` password assigned to it.
  34. .. code-block:: bash
  35. su postgres -c "createuser -P -d -r -s docker"
  36. Create a test database also named ``docker`` owned by previously created ``docker``
  37. role.
  38. .. code-block:: bash
  39. su postgres -c "createdb -O docker docker"
  40. Adjust PostgreSQL configuration so that remote connections to the
  41. database are possible. Make sure that inside
  42. ``/etc/postgresql/9.3/main/pg_hba.conf`` you have following line:
  43. .. code-block:: bash
  44. host all all 0.0.0.0/0 md5
  45. Additionaly, inside ``/etc/postgresql/9.3/main/postgresql.conf``
  46. uncomment ``listen_addresses`` like so:
  47. .. code-block:: bash
  48. listen_addresses='*'
  49. .. note::
  50. This PostgreSQL setup is for development only purposes. Refer
  51. to PostgreSQL documentation how to fine-tune these settings so that it
  52. is enough secure.
  53. Exit.
  54. .. code-block:: bash
  55. exit
  56. Create an image from our container and assign it a name. The ``<container_id>``
  57. is in the Bash prompt; you can also locate it using ``docker ps -a``.
  58. .. code-block:: bash
  59. sudo docker commit <container_id> <your username>/postgresql
  60. Finally, run the PostgreSQL server via ``docker``.
  61. .. code-block:: bash
  62. CONTAINER=$(sudo docker run -d -p 5432 \
  63. -t <your username>/postgresql \
  64. /bin/su postgres -c '/usr/lib/postgresql/9.3/bin/postgres \
  65. -D /var/lib/postgresql/9.3/main \
  66. -c config_file=/etc/postgresql/9.3/main/postgresql.conf')
  67. Connect the PostgreSQL server using ``psql`` (You will need PostgreSQL installed
  68. on the machine. For Ubuntu, use something like
  69. ``sudo apt-get install postgresql``).
  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.3.1)
  76. Type "help" for help.
  77. docker=# CREATE DATABASE foo OWNER=docker;
  78. CREATE DATABASE
  79. Additionally, publish your newly created image on the 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 -run='{"Cmd": \
  93. ["/bin/su", "postgres", "-c", "/usr/lib/postgresql/9.3/bin/postgres -D \
  94. /var/lib/postgresql/9.3/main -c \
  95. config_file=/etc/postgresql/9.3/main/postgresql.conf"], "PortSpecs": ["5432"]}' \
  96. <container_id> <your username>/postgresql
  97. From now on, just type ``docker run <your username>/postgresql`` and
  98. PostgreSQL should automatically start.