PostgreSQL with Docker: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(40 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Internal=
=Internal=


* [[Postgresql#Subjects|Postgresql]]
* [[PostgreSQL#Subjects|PostgreSQL]]


=Overview=
=Overview=


A transient Posgres container instance can be started and exposed on a local host port as shown in the [[#Running_a_Transient_Instance|Running a Transient Instance]] section.
This article describes how to stand up and operate a Docker-based PostgreSQL instance. The instance will be accessible on the standard PostgreSQL port on the local host and it will be secured by a conventional user and password. Since the password is visible in the terminal used to create the container image, this approach is not really secure, but it is good enough for development purposes. The container-generated data will be stored on an [[Docker_Storage_Concepts#Anonymous_Volume|anonymous]] [[Docker_Storage_Concepts#Local_Volume_Driver|local]] volume.


<font color=darkgray>
=Operations=
* simplest image - data is lost
* external volume
* port mapping.
</font>


=Running a Transient Instance=
==Create the Container==


Postgres binds by default on port 5432 and this is the port published by the following command:
This is an one-time operation:


<syntaxhighlight lang='bash'>
docker run -p 5432:5432/tcp -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=<some-password> --name postgres postgres
docker run -p 5432:5432/tcp postgres
</syntaxhighlight>


The command will implicitly create a [[Docker_Storage_Concepts#Local_Volume_Driver|local]] [[Docker_Storage_Concepts#Anonymous_Volume|anonymous]] volume that will linger around after the container stops. The data on the anonymous local volume can be accessed by starting the stopped container. <font color=red>Test</font>
If you want to use a local directory for storage, which will be made available to the container with a [[Docker_Storage_Concepts#Bind_Mount|bind mount]], add the following arguments. Note that this is only necessary if you want to have convenient access to the database files and logs, for troubleshooting and debugging purposes:


You can attach to the container with:
--mount type=bind,source=<local-directory>,target=/var/lib/postgresql/data


<syntaxhighlight lang='bash'>
==Start the Container==
docker exec -it <container-id> bash
 
</syntaxhighlight>
docker start postgres
 
==Stop the Container==
 
docker stop postgres
 
==Access Container Logs==
 
docker logs -f postgres

Latest revision as of 20:20, 19 October 2018

Internal

Overview

This article describes how to stand up and operate a Docker-based PostgreSQL instance. The instance will be accessible on the standard PostgreSQL port on the local host and it will be secured by a conventional user and password. Since the password is visible in the terminal used to create the container image, this approach is not really secure, but it is good enough for development purposes. The container-generated data will be stored on an anonymous local volume.

Operations

Create the Container

This is an one-time operation:

docker run -p 5432:5432/tcp -e POSTGRES_USER=admin -e POSTGRES_PASSWORD=<some-password> --name postgres postgres

If you want to use a local directory for storage, which will be made available to the container with a bind mount, add the following arguments. Note that this is only necessary if you want to have convenient access to the database files and logs, for troubleshooting and debugging purposes:

--mount type=bind,source=<local-directory>,target=/var/lib/postgresql/data

Start the Container

docker start postgres

Stop the Container

docker stop postgres

Access Container Logs

docker logs -f postgres