postgresql_service.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. .. _this blog post: http://zaiste.net/2013/08/docker_postgresql_how_to/
  10. Installing PostgreSQL on Docker
  11. -------------------------------
  12. For clarity I won't be showing commands output.
  13. Run an interactive shell in Docker container.
  14. .. code-block:: bash
  15. docker run -i -t base /bin/bash
  16. Update its dependencies.
  17. .. code-block:: bash
  18. apt-get update
  19. Install ``python-software-properies``.
  20. .. code-block:: bash
  21. apt-get install python-software-properties
  22. apt-get install software-properties-common
  23. Add Pitti's PostgreSQL repository. It contains the most recent stable release
  24. of PostgreSQL i.e. ``9.2``.
  25. .. code-block:: bash
  26. add-apt-repository ppa:pitti/postgresql
  27. apt-get update
  28. Finally, install PostgreSQL 9.2
  29. .. code-block:: bash
  30. apt-get -y install postgresql-9.2 postgresql-client-9.2 postgresql-contrib-9.2
  31. Now, create a PostgreSQL superuser role that can create databases and other roles.
  32. Following Vagrant's convention the role will be named `docker` with `docker`
  33. password assigned to it.
  34. .. code-block:: bash
  35. sudo -u postgres 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. sudo -u postgres createdb -O docker docker
  40. Adjust PostgreSQL configuration so that remote connections to the database are
  41. possible. Make sure that inside ``/etc/postgresql/9.2/main/pg_hba.conf`` you have
  42. following line:
  43. .. code-block:: bash
  44. host all all 0.0.0.0/0 md5
  45. Additionaly, inside ``/etc/postgresql/9.2/main/postgresql.conf`` uncomment
  46. ``listen_address`` so it is as follows:
  47. .. code-block:: bash
  48. listen_address='*'
  49. *Note:* this PostgreSQL setup is for development only purposes. Refer to
  50. PostgreSQL documentation how to fine-tune these settings so that it is enough
  51. secure.
  52. Create an image and assign it a name. ``<container_id>`` is in the Bash prompt;
  53. you can also locate it using ``docker ps -a``.
  54. .. code-block:: bash
  55. docker commit <container_id> <your username>/postgresql
  56. Finally, run PostgreSQL server via ``docker``.
  57. .. code-block:: bash
  58. CONTAINER=$(docker run -d -p 5432 \
  59. -t <your username>/postgresql \
  60. /bin/su postgres -c '/usr/lib/postgresql/9.2/bin/postgres \
  61. -D /var/lib/postgresql/9.2/main \
  62. -c config_file=/etc/postgresql/9.2/main/postgresql.conf')
  63. Connect the PostgreSQL server using ``psql``.
  64. .. code-block:: bash
  65. CONTAINER_IP=$(docker inspect $CONTAINER | grep IPAddress | awk '{ print $2 }' | tr -d ',"')
  66. psql -h $CONTAINER_IP -p 5432 -d docker -U docker -W
  67. As before, create roles or databases if needed.
  68. .. code-block:: bash
  69. psql (9.2.4)
  70. Type "help" for help.
  71. docker=# CREATE DATABASE foo OWNER=docker;
  72. CREATE DATABASE
  73. Additionally, publish there your newly created image on Docker Index.
  74. .. code-block:: bash
  75. docker login
  76. Username: <your username>
  77. [...]
  78. .. code-block:: bash
  79. docker push <your username>/postgresql
  80. PostgreSQL service auto-launch
  81. ------------------------------
  82. Running our image seems complicated. We have to specify the whole command with
  83. ``docker run``. Let's simplify it so the service starts automatically when the
  84. container starts.
  85. .. code-block:: bash
  86. docker commit <container_id> <your username>/postgresql -run='{"Cmd": \
  87. ["/bin/su", "postgres", "-c", "/usr/lib/postgresql/9.2/bin/postgres -D \
  88. /var/lib/postgresql/9.2/main -c \
  89. config_file=/etc/postgresql/9.2/main/postgresql.conf"], PortSpecs": ["5432"]}
  90. From now on, just type ``docker run <your username>/postgresql`` and PostgreSQL
  91. should automatically start.