How can we help?
Categories
< All Topics
Print

Setting Up PostgreSQL with Docker on Server Stadium Cloud VMs or Dedicated Servers

Introduction

PostgreSQL is a powerful, open-source object-relational database system known for its robustness, scalability, and performance. Using Docker to deploy PostgreSQL can simplify the setup and maintenance of your database environment. This guide will walk you through setting up a PostgreSQL database using Docker on Server Stadium infrastructure.

Prerequisites

  • Access to a Server Stadium cloud VM or dedicated server with Docker installed. If you need to install Docker, sign up here and refer to the official Docker page for installation instructions.
  • At least 2GB of RAM on your server for optimal performance.
  • Administrative or sudo privileges on your server.

Step 1: Pull the PostgreSQL Docker Image

  1. Download the PostgreSQL Image
    Pull the latest PostgreSQL image from the Docker Hub:

    docker pull postgres

Step 2: Run PostgreSQL in a Docker Container

  1. Start the PostgreSQL Container
    Run a PostgreSQL container with a specified username, password, and database name:

    docker run –name mypostgres -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypass -e POSTGRES_DB=mydb -p 5432:5432 -d postgres

    This command sets environment variables for the user, password, and database, and maps port 5432 on the host to port 5432 in the container.

Step 3: Configure Persistent Data Storage

  1. Create a Docker Volume
    To ensure data persistence across container restarts, create a Docker volume:

    docker volume create pgdata

  2. Attach the Volume to the PostgreSQL Container
    Start the PostgreSQL container with the volume attached:

    docker run –name mypostgres -e POSTGRES_USER=myuser -e POSTGRES_PASSWORD=mypass -e POSTGRES_DB=mydb -p 5432:5432 -v pgdata:/var/lib/postgresql/data -d postgres

Step 4: Access and Manage the PostgreSQL Database

  1. Connect to the PostgreSQL Database
    Connect to your PostgreSQL database using psql or any database management tool:

    psql -h localhost -p 5432 -U myuser -d mydb

    You will be prompted to enter the password specified earlier.

Step 5: Backup and Restore

  1. Backup the PostgreSQL Database
    Backup your database using the pg_dump tool:

    docker exec mypostgres pg_dump -U myuser mydb > mydb_backup.sql

  2. Restore the PostgreSQL Database
    Restore your database using psql:

    cat mydb_backup.sql | docker exec -i mypostgres psql -U myuser -d mydb

Conclusion

You have successfully set up a PostgreSQL database using Docker on your Server Stadium cloud VM or dedicated server. This setup not only ensures your data is managed efficiently but also leverages Docker for ease of deployment and scalability.

For more information on managing your PostgreSQL installation or other Docker-based applications, visit our knowledge base or consult the Server Stadium website.

Table of Contents