postgresql_service.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. Installing PostgreSQL on Docker
  9. -------------------------------
  10. Assuming there is no Docker image that suits your needs in `the index`_, you
  11. can create one yourself.
  12. .. _the index: http://index.docker.io
  13. Start by creating a new Dockerfile:
  14. .. note::
  15. This PostgreSQL setup is for development only purposes. Refer
  16. to the PostgreSQL documentation to fine-tune these settings so that it
  17. is suitably secure.
  18. .. literalinclude:: postgresql_service.Dockerfile
  19. Build an image from the Dockerfile assign it a name.
  20. .. code-block:: bash
  21. $ sudo docker build -t eg_postgresql .
  22. And run the PostgreSQL server container (in the foreground):
  23. .. code-block:: bash
  24. $ sudo docker run --rm -P --name pg_test eg_postgresql
  25. There are 2 ways to connect to the PostgreSQL server. We can use
  26. :ref:`working_with_links_names`, or we can access it from our host (or the network).
  27. .. note:: The ``--rm`` removes the container and its image when the container
  28. exists successfully.
  29. Using container linking
  30. ^^^^^^^^^^^^^^^^^^^^^^^
  31. Containers can be linked to another container's ports directly using
  32. ``--link remote_name:local_alias`` in the client's ``docker run``. This will
  33. set a number of environment variables that can then be used to connect:
  34. .. code-block:: bash
  35. $ sudo docker run --rm -t -i --link pg_test:pg eg_postgresql bash
  36. postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password
  37. Connecting from your host system
  38. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  39. Assuming you have the postgresql-client installed, you can use the host-mapped port
  40. to test as well. You need to use ``docker ps`` to find out what local host port the
  41. container is mapped to first:
  42. .. code-block:: bash
  43. $ docker ps
  44. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  45. 5e24362f27f6 eg_postgresql:latest /usr/lib/postgresql/ About an hour ago Up About an hour 0.0.0.0:49153->5432/tcp pg_test
  46. $ psql -h localhost -p 49153 -d docker -U docker --password
  47. Testing the database
  48. ^^^^^^^^^^^^^^^^^^^^
  49. Once you have authenticated and have a ``docker =#`` prompt, you can
  50. create a table and populate it.
  51. .. code-block:: bash
  52. psql (9.3.1)
  53. Type "help" for help.
  54. docker=# CREATE TABLE cities (
  55. docker(# name varchar(80),
  56. docker(# location point
  57. docker(# );
  58. CREATE TABLE
  59. docker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
  60. INSERT 0 1
  61. docker=# select * from cities;
  62. name | location
  63. ---------------+-----------
  64. San Francisco | (-194,53)
  65. (1 row)
  66. Using the container volumes
  67. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  68. You can use the defined volumes to inspect the PostgreSQL log files and to backup your
  69. configuration and data:
  70. .. code-block:: bash
  71. docker run --rm --volumes-from pg_test -t -i busybox sh
  72. / # ls
  73. bin etc lib linuxrc mnt proc run sys usr
  74. dev home lib64 media opt root sbin tmp var
  75. / # ls /etc/postgresql/9.3/main/
  76. environment pg_hba.conf postgresql.conf
  77. pg_ctl.conf pg_ident.conf start.conf
  78. /tmp # ls /var/log
  79. ldconfig postgresql