Dockerize PostgreSQL

Installing PostgreSQL on Docker

Assuming there is no Docker image that suits your needs on the Docker Hub, you can create one yourself.

Start by creating a new Dockerfile:

Note: This PostgreSQL setup is for development-only purposes. Refer to the PostgreSQL documentation to fine-tune these settings so that it is suitably secure.

  1.  
  1. #
  2. # example Dockerfile for https://docs.docker.com/engine/examples/postgresql_service/
  3. #
  4.  
  5. FROM ubuntu
  6.  
  7. # Add the PostgreSQL PGP key to verify their Debian packages.
  8. # It should be the same key as https://www.postgresql.org/media/keys/ACCC4CF8.asc
  9. RUN apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
  10.  
  11. # Add PostgreSQL's repository. It contains the most recent stable release
  12. # of PostgreSQL, ``9.3``.
  13. RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" > /etc/apt/sources.list.d/pgdg.list
  14.  
  15. # Install ``python-software-properties``, ``software-properties-common`` and PostgreSQL 9.3
  16. # There are some warnings (in red) that show up during the build. You can hide
  17. # them by prefixing each apt-get statement with DEBIAN_FRONTEND=noninteractive
  18. RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3
  19.  
  20. # Note: The official Debian and Ubuntu images automatically ``apt-get clean``
  21. # after each ``apt-get``
  22.  
  23. # Run the rest of the commands as the ``postgres`` user created by the ``postgres-9.3`` package when it was ``apt-get installed``
  24. USER postgres
  25.  
  26. # Create a PostgreSQL role named ``docker`` with ``docker`` as the password and
  27. # then create a database `docker` owned by the ``docker`` role.
  28. # Note: here we use ``&&\`` to run commands one after the other - the ``\``
  29. # allows the RUN command to span multiple lines.
  30. RUN /etc/init.d/postgresql start &&\
  31. psql --command "CREATE USER docker WITH SUPERUSER PASSWORD 'docker';" &&\
  32. createdb -O docker docker
  33.  
  34. # Adjust PostgreSQL configuration so that remote connections to the
  35. # database are possible.
  36. RUN echo "host all all 0.0.0.0/0 md5" >> /etc/postgresql/9.3/main/pg_hba.conf
  37.  
  38. # And add ``listen_addresses`` to ``/etc/postgresql/9.3/main/postgresql.conf``
  39. RUN echo "listen_addresses='*'" >> /etc/postgresql/9.3/main/postgresql.conf
  40.  
  41. # Expose the PostgreSQL port
  42. EXPOSE 5432
  43.  
  44. # Add VOLUMEs to allow backup of config, logs and databases
  45. VOLUME ["/etc/postgresql", "/var/log/postgresql", "/var/lib/postgresql"]
  46.  
  47. # Set the default command to run when starting the container
  48. CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]

  

Build an image from the Dockerfile assign it a name.

  1. $ docker build -t eg_postgresql .

 

Run the PostgreSQL server container (in the foreground):

  1. $ docker run --rm -P --name pg_test eg_postgresql

  

There are 2 ways to connect to the PostgreSQL server. We can use Link Containers, or we can access it from our host (or the network).

Note: The --rm removes the container and its image when the container exits successfully.

Using container linking

Containers can be linked to another container’s ports directly using -link remote_name:local_alias in the client’s docker run. This sets a number of environment variables that can then be used to connect:

  1. $ docker run --rm -t -i --link pg_test:pg eg_postgresql bash
  2.  
  3. postgres@7ef98b1b7243:/$ psql -h $PG_PORT_5432_TCP_ADDR -p $PG_PORT_5432_TCP_PORT -d docker -U docker --password

  

Connecting from your host system

Assuming you have the postgresql-client installed, you can use the host-mapped port to test as well. You need to use docker ps to find out what local host port the container is mapped to first:

  1. $ docker ps
  2.  
  3. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  4. 5e24362f27f6 eg_postgresql:latest /usr/lib/postgresql/ About an hour ago Up About an hour 0.0.0.0:49153->5432/tcp pg_test
  5.  
  6. $ psql -h localhost -p 49153 -d docker -U docker --password

  

Testing the database

Once you have authenticated and have a docker =# prompt, you can create a table and populate it.

  1. psql (9.3.1)
  2. Type "help" for help.
  3.  
  4. $ docker=# CREATE TABLE cities (
  5. docker(# name varchar(80),
  6. docker(# location point
  7. docker(# );
  8. CREATE TABLE
  9. $ docker=# INSERT INTO cities VALUES ('San Francisco', '(-194.0, 53.0)');
  10. INSERT 0 1
  11. $ docker=# select * from cities;
  12. name | location
  13. ---------------+-----------
  14. San Francisco | (-194,53)
  15. (1 row)

  

Using the container volumes

You can use the defined volumes to inspect the PostgreSQL log files and to backup your configuration and data:

  1. $ docker run --rm --volumes-from pg_test -t -i busybox sh
  2.  
  3. / # ls
  4. bin etc lib linuxrc mnt proc run sys usr
  5. dev home lib64 media opt root sbin tmp var
  6. / # ls /etc/postgresql/9.3/main/
  7. environment pg_hba.conf postgresql.conf
  8. pg_ctl.conf pg_ident.conf start.conf
  9. /tmp # ls /var/log
  10. ldconfig postgresql

  

 

Dockerize PostgreSQL的更多相关文章

  1. postgresql 基本语法

    postgresql数据库创建/修改/删除等写入类代码语法总结: 1,创建库 2,创建/删除表 2.1 创建表 create table myTableName 2.2 如果表不存在则创建表 crea ...

  2. postgresql无法安装pldbgapi的问题

    要对函数进行调试需要安装插件pldbgapi,当初在windows上面的postgresql实例中执行了一下语句就安装上了: create extension pldbgapi; 但是在linux中执 ...

  3. ASP.NET MVC 使用 Petapoco 微型ORM框架+NpgSql驱动连接 PostgreSQL数据库

    前段时间在园子里看到了小蝶惊鸿 发布的有关绿色版的Linux.NET——“Jws.Mono”.由于我对.Net程序跑在Linux上非常感兴趣,自己也看了一些有关mono的资料,但是一直没有时间抽出时间 ...

  4. MongoDB与PostgresQL无责任初步测试

    PostgresQL一秒能插入多少条记录,MongoDB呢?读取的情况又如何?我写了一些简单的程序,得出了一些简单的数据,贴在这里分享,继续往下阅读前请注意下本文标题中的“无责任”,这表示此测试结果不 ...

  5. [PostgreSQL] 图解安装 PostgreSQL

    图解安装 PostgreSQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5894462.html 序 园友的一篇<Asp.Net Cor ...

  6. Asp.Net Core 项目实战之权限管理系统(3) 通过EntityFramework Core使用PostgreSQL

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  7. PostgreSQL介绍以及如何开发框架中使用PostgreSQL数据库

    最近准备下PostgreSQL数据库开发的相关知识,本文把总结的PPT内容通过博客记录分享,本随笔的主要内容是介绍PostgreSQL数据库的基础信息,以及如何在我们的开发框架中使用PostgreSQ ...

  8. PostgreSql性能测试

    # PostgreSql性能测试 ## 1. 环境+ 版本:9.4.9+ 系统:OS X 10.11.5+ CPU:Core i5 2.7G+ 内存:16G+ 硬盘:256G SSD ## 2. 测试 ...

  9. postgresql 导出数据字典文档

    项目上需要整理目前数据库的数据字典文档.项目不规范,这种文档只要后期来补.这么多张表,每个字段都写到word文档里真心头大.就算前面写了个查询表结构的sql,但是最后整理到word里还是感觉有点麻烦. ...

随机推荐

  1. uva 10369 Arctic Network

    题意: 有许多基地,每个基地都有两种收发信号的方式,一种是通过无线电收发机,另一种是通过卫星.两个基地之间可以通过卫星交流不管它们相距多远:但是通过无线电交流,就要求它们的距离不超过D.为了方便布置, ...

  2. mysql 安装目录说明

  3. .pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法

    .pages怎么在windows上打开?Windows下打开在Mac中编辑的.pages文件方法 1.最简单的方法是修改后缀名为.zip然后解压,解压后就可以看到一张图片,这个就是文档内容了. 2.更 ...

  4. Symfony2 学习笔记之控制器

    一个controller是你创建的一个PHP函数,它接收HTTP请求(request)并创建和返回一个HTTP回复(Response).回复对象(Response)可以是一个HTML页面,一个XML文 ...

  5. Python+OpenCV图像处理(七)—— 滤波与模糊操作

    过滤是信号和图像处理中基本的任务.其目的是根据应用环境的不同,选择性的提取图像中某些认为是重要的信息.过滤可以移除图像中的噪音.提取感兴趣的可视特征.允许图像重采样等等.频域分析将图像分成从低频到高频 ...

  6. javascript 的01

    javaScript可以实现验证表单.制作特效等功能,JavaScript的目的主要是一下三点: 1 客户端表单验证2 页面动态效果3 jQuery的基础JavaScript是一种描述性语言,也是一种 ...

  7. 八大排序算法之七—堆排序(Heap Sort)

    堆排序是一种树形选择排序,是对直接选择排序的有效改进. 基本思想: 堆的定义如下:具有n个元素的序列(k1,k2,...,kn),当且仅当满足 时称之为堆.由堆的定义可以看出,堆顶元素(即第一个元素) ...

  8. HFA and outhandler differentiate or not

    better trouble shooting auto-open accidently? HFA not stable? check code modification ASAP!!!

  9. gcahce事物不够,借助binlog追上

    gcahce事物不够,借助binlog追上 宕机节点以单机集群启动,既自己作为一个集群启动,不过UUID要和旧的集群保持一致: 修复grastate.dat 文件的方式这里略,直接通过wsrep_re ...

  10. 关于functools模块的wraps装饰器用途

    测试环境:Python3.6.2 + win10 +  Pycharm2017.3 装饰器之functools模块的wraps的用途: 首先我们先写一个装饰器 # 探索functools模块wraps ...