Install and Run PostgreSQL with Docker and Docker Compose

Tutorial: Installing PostgreSQL with Docker Compose

Install and Run PostgreSQL with Docker and Docker Compose

PostgreSQL is a powerful, open-source relational database management system (RDBMS) known for its robustness, extensibility, and standards compliance.

It supports a wide variety of data types and has advanced features such as transactional integrity, concurrency control, and a rich set of data manipulation functions. PostgreSQL is widely used in various applications due to its reliability, scalability, and active development community.

Popular Apps Built with PostgreSQL:

  1. Instagram: This widely-used social media platform relies on PostgreSQL for handling its vast amounts of user data, photos, and interactions.
  2. Spotify: The music streaming service uses PostgreSQL to manage its extensive library of songs, playlists, and user data, ensuring quick access and seamless performance.
  3. Reddit: Known as "the front page of the internet," Reddit uses PostgreSQL to manage its high-traffic content and user interactions efficiently.
  4. TripAdvisor: This travel and restaurant review website employs PostgreSQL to store and manage a large volume of user-generated content and reviews.
  5. Uber: The ride-hailing giant uses PostgreSQL as part of its technology stack to handle complex data queries and ensure reliable service.
  6. Disqus: This popular commenting system uses PostgreSQL to manage comments, user profiles, and other interaction data across millions of websites.

PostgreSQL's versatility and performance make it a preferred choice for both startups and large enterprises, enabling them to build scalable and reliable applications.

Install PostgreSQL with Docker and Docker Compose

This tutorial will guide you through the process of installing PostgreSQL using Docker Compose. This method simplifies the setup and management of PostgreSQL instances, making it ideal for development and testing environments.

Prerequisites

  • Docker installed on your machine
  • Docker Compose installed on your machine
  • Basic knowledge of Docker and Docker Compose

Step 1: Create a Directory for Your Project

First, create a directory for your PostgreSQL project. Open a terminal and run:

mkdir postgres-docker
cd postgres-docker

Step 2: Create a docker-compose.yml File

In your project directory, create a docker-compose.yml file. This file will define the PostgreSQL service.

version: '3.8'

services:
  db:
    image: postgres:latest
    container_name: postgres_container
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: mydatabase
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:

This configuration sets up a PostgreSQL service with:

  • User: myuser
  • Password: mypassword
  • Database: mydatabase
  • Data persisted in a Docker volume postgres_data
  • Port 5432 exposed on the host machine

Step 3: Start the PostgreSQL Service

Run the following command to start the PostgreSQL service:

docker-compose up -d

The -d flag runs the service in detached mode.

Step 4: Verify the Installation

To verify that PostgreSQL is running correctly, use the following command to list the running containers:

docker ps

You should see an entry for postgres_container.

Step 5: Connect to PostgreSQL

You can connect to the PostgreSQL database using a client like psql. To connect from the host machine, use:

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

You'll be prompted to enter the password (mypassword).

Alternatively, you can connect to the PostgreSQL container and use psql inside it:

docker exec -it postgres_container psql -U myuser -d mydatabase

Step 6: Manage the PostgreSQL Service

To stop the PostgreSQL service, run:

docker-compose down

This command stops and removes the containers but preserves the data in the postgres_data volume.

To start the service again, use:

docker-compose up -d

Conclusion

You've successfully installed PostgreSQL using Docker Compose. This setup is highly configurable and can be adapted to suit various development and testing needs. For more advanced configurations, refer to the official Docker and PostgreSQL documentation.







Read more




Open-source Apps

9,500+

Medical Apps

500+

Lists

450+

Dev. Resources

900+

/