Skip to main content

How to Install Postgres on Docker

Docker is an open-source platform where we can create, deploy, and run applications by using containers. Docker is similar to virtual machines (VM), but in VM you need to create a virtual operating system, while Docker allows applications to use the system kernel.

PostgreSQL community supports (v9.4,v9.5, v9.6, v10, v11 etc.) Images.

The difference between Images and containers is that Images are simply templates of instructions, and an instance of an Image is called a container.


Prerequisites

  • Access to a command line/terminal window
  • A user account with sudo privileges
  • An existing Docker installation


Option 1:
Run Postgres Using Docker Compose

To deploy a Postgres container using Docker Compose, you should have this Docker tool set up on your system.

If you are a Linux user and need help setting up, refer to one of our guides on installing Docker Compose on Ubuntu or how to install Docker Compose on CentOS.

1. To ensure an easy and clean installation, we first want to create a working directory named postgres and move into that directory:

mkdir postgres
cd postgres/

2. Next, use Docker Compose to download the Postgres image and get the service up and running. Do this by creating a new docker-compose.yml file with an editor of your choice (in this example, we used nano):

nano docker-compose.yml

3. Add the following content to the docker-compose file:

version: ‘3’
service:
  postgres:
  image: ‘postgres: latest’
  ports:
    - “5432:5432” 

The yaml configuration file outlines there is a postgres service, built on the latest postgres image. You can decide on the newest Postgres version or specify the version number you want to use.

Finally, you need to define the ports on which the container communicates. 5432 is the default port number for PostgreSQL.

4. Save and exit the file.

5. Now that you have the yaml configuration file, you can start the postgres service and run the container. Use the docker-compose up command with the -d option to put it into detach mode (allowing you to continue to run commands from the current shell):

docker-compose up -d

6. You can check the logs with the command:

docker-compose logs -f

To return to the shell press CTRL+C.

Option 2:
Run Postgres Using a Single Docker Command

Another way to deploy PostgreSQL in a container is by running a single docker command.

You can download and run a Postgres container by specifying all the necessary information in one command.

docker run --name [container_name] -e POSTGRES_PASSWORD=[your_password] -d postgres

The command tells Docker to run a new container under a particular container name, defines the Postgres password, and downloads the latest Postgres release.

Confirm your PostgreSQL container is now up by prompting Docker to list all running containers with:

docker ps

In this guide, we created a container named example and we can quickly locate it among other running containers.

A single command for deploying Postgres in a Docker container.

Starting with Postgres Containers

Connect to Postgres in Docker Container

To enter a Postgres container, you need to execute using the container name and enable psql, the command-line interface for Postgres.

docker exec -it [container_name] psql -U [postgres_user]

In the example below, we connected to the example container as the postgres user.

Command for connecting to Postgres in a Docker container.

Create a Database

Once in the Docker Postgres container, we can create a database with:

create database [db_name];


Note: 
To view all the databases you have running on PostgreSQL run: \l.


Connect to the database as the postgres user type:

\c [db_name]

With the database set up, the next step is to create a schema that helps you get a logical representation of the database structure:

create schema [db_schema_name]

Here you can create a table and add data into the table.

create table [table_name] ([field_names] [values])






Comments

Popular posts from this blog

How to Reset Steam Password? – Recover Your Steam Password

Are you suffering with Reset Steam Password? This blog will help you. What is Steam? Steam is the online website for getting the best games for paid. If you are a true game lover then you should have an active account on Steam. Many of people lost their password during the  PC clean-up  or any other circumstances. At that time they have to reset Password of all accounts. Then if you facing issues with the password, you forced to Steam reset password to access the Steam account. We are going to share the guide that how to recover your forgotten Steam Password and make your existing account ready. First of all, you should have enough knowledge about what is Steam and why Steam Account required? Why Steam Account Requires? Before jump into password recovery and Login details, let me clear about why Steam Account required? There are several benefits of this Steam Account from where you can easily download PC games as well as software. Though it will be p...

What is STP? - Explain Advantages and Disadvantages

The Spanning Tree Protocol is a network protocol that builds a loop-free logical topology for Ethernet networks. The basic function of STP is to prevent bridge loops and the broadcast radiation that results from them. STP is a protocol. It actively monitors all links of the network. To finds a redundant link, it uses an algorithm, known as the STA (spanning-tree algorithm). The STA algorithm first creates a topology database then it finds and disables the redundant links. Once redundant links are disabled, only the STP-chosen links remain active. If a new link is added or an existing link is removed, the STP re-runs the STA algorithm and re-adjusts all links to reflect the change. STP (Spanning Tree Protocol) automatically removes layer 2 switching loops by shutting down the redundant links. A redundant link is an additional link between two switches. A redundant link is usually created for backup purposes. Just like every coin has two sides, a redundant link, along with...