NiketTongare

Updated at 07 June 2024

Steps to Setup PostgreSQL in a Low-End Server Using Docker.

A comprehensive guide to setting up PostgreSQL on a low-end server using Docker, from installing Docker to configuring PostgreSQL with Docker Compose.

Technology

10 views

Steps to Setup PostgreSQL in a Low-End Server Using Docker.

Setting up a database on a low-end server can be challenging, but using Docker simplifies the process. This guide will walk you through setting up PostgreSQL on an Ubuntu server with limited resources using Docker. Follow these steps to ensure a smooth and efficient installation.

Server Specifications:

  • CPU: 2 vCPU
  • RAM: 2 GB
  • Storage: 40 GB

Step 1: Update and Install Required Packages

Before installing Docker, update your package list and install the necessary packages.

sudo apt-get update
sudo apt-get install ca-certificates curl

Step 2: Add Docker's Official GPG Key

To ensure the authenticity of the Docker packages, add Docker's official GPG key to your system.

sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

Step 3: Add the Docker Repository to Apt Sources

Next, add Docker’s repository to your system’s package sources. This allows you to install Docker from Docker's official repository.

echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

Step 4: Install Docker

With the repository added, you can now install Docker and its associated components.

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 5: Create a Docker Volume for PostgreSQL

To persist PostgreSQL data, create a Docker volume. This ensures your data remains intact even if the container is removed or updated.

docker volume create pgdata

Step 6: Create and Run the Docker Compose File

Create a Docker Compose file to define the PostgreSQL service. This file specifies the configuration and environment for PostgreSQL.

Create a file named docker-compose.yml and add the following content:

version: '3.8'

services:
postgres:
image: postgres
ports:
- "5450:5432"
restart: always
container_name: portfolio-postgres
environment:
POSTGRES_USER: postgres
POSTGRES_DB: portfolio-db
POSTGRES_PASSWORD: porfolio@db
POSTGRES_HOST_AUTH_METHOD: trust
volumes:
- pgdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 10s
timeout: 5s
retries: 5
postgres_is_ready:
image: postgres
depends_on:
postgres:
condition: service_healthy

volumes:
pgdata:
external: true

This file configures a PostgreSQL container with the following details:

  • Image: postgres (the official PostgreSQL image)
  • Ports: Maps port 5450 on the host to port 5432 in the container
  • Restart Policy: Always restart the container if it stops
  • Container Name: portfolio-postgres
  • Environment Variables: Sets the PostgreSQL user, database, and password
  • Volumes: Persists data to the Docker volume pgdata
  • Healthcheck: Monitors the PostgreSQL service to ensure it is ready
  • Dependency: Ensures that postgres_is_ready service waits until PostgreSQL is healthy

Step 7: Launch PostgreSQL

Finally, use Docker Compose to start the PostgreSQL service.

sudo docker compose up -d

This command will download the necessary images and start the PostgreSQL service in the background.

Step 8: Connect to PostgreSQL

Once the PostgreSQL service is running, you can connect to the database using a PostgreSQL client.

Connection Details:

  • Host: localhost (or the server’s IP address if accessing remotely)
  • Port: 5450
  • Username: postgres
  • Password: porfolio@db
  • Database Name: portfolio-db

You can use the psql command-line tool or any PostgreSQL client application to connect. Here’s an example using psql:

psql -h localhost -p 5450 -U postgres -d portfolio-db

When prompted, enter the password: porfolio@db.

By following these steps, you've successfully set up PostgreSQL on a low-end server using Docker. This setup ensures your database is efficiently managed, even with limited system resources.





Author: Niket Tongare

#steps
#to
#setup
#postgresql
#in
#a
#low
#end
#server
#using
#docker
#comprehensive
#guide
#setting
#up
#on
#from
#installing
#configuring
#with
#compose

Made with love by

@nikettongare