Docker and PostgreSQL in [10 Minutes]

3 min read 7 months ago
Published on Aug 12, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial will guide you through the process of using Docker with PostgreSQL to create a flexible development environment. With Docker, you can quickly spin up database instances that work consistently across different setups, eliminating the common frustration of "It works on my machine." By the end of this tutorial, you will know how to set up a PostgreSQL container and connect to it from both the terminal and a Spring Boot application.

Step 1: Install Docker

Before you can use Docker and PostgreSQL, you need to have Docker installed on your machine.

  • For Windows or Mac:

    • Download Docker Desktop from the Docker website.
    • Follow the installation instructions for your operating system.
  • For Linux:

    • Use your package manager to install Docker. For example, on Ubuntu, you can run:
      sudo apt update
      sudo apt install docker.io
      
  • After installation, verify it by running:

    docker --version
    

Step 2: Pull the PostgreSQL Docker Image

Now that Docker is installed, you need to download the PostgreSQL image.

  • Open your terminal or command prompt and run:
    docker pull postgres
    
  • This command downloads the latest PostgreSQL image from Docker Hub.

Step 3: Run the PostgreSQL Container

Next, you will create and run a PostgreSQL container from the image you just downloaded.

  • Use the following command to run the container:
    docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres
    
    • --name my-postgres: Assigns a name to your container.
    • -e POSTGRES_PASSWORD=mysecretpassword: Sets the password for the PostgreSQL user.
    • -d: Runs the container in detached mode (in the background).
    • -p 5432:5432: Maps port 5432 of the container to port 5432 on your host.

Step 4: Connect to PostgreSQL from the Terminal

You can connect to your PostgreSQL database using the psql command-line tool.

  • Open a new terminal window and run:
    psql -h localhost -U postgres
    
  • Enter the password you set earlier (mysecretpassword).
  • You should now be in the PostgreSQL command line interface.

Step 5: Create a Database

Once connected, you can create a new database for your application.

  • To create a database, run:
    CREATE DATABASE mydb;
    
  • Verify the database creation by listing all databases:
    \l
    

Step 6: Connect to PostgreSQL from a Spring Boot Application

If you're using Spring Boot, you can connect to your PostgreSQL database by configuring your application.properties or application.yml.

  • Add the following properties to application.properties:

    spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
    spring.datasource.username=postgres
    spring.datasource.password=mysecretpassword
    spring.jpa.hibernate.ddl-auto=update
    
  • Ensure you have the PostgreSQL dependency in your pom.xml:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
    </dependency>
    

Conclusion

You have successfully set up PostgreSQL using Docker and learned how to connect to it from both the terminal and a Spring Boot application. This setup streamlines your development process and ensures consistency across different environments.

Next Steps

  • Explore advanced PostgreSQL features.
  • Learn how to manage Docker containers effectively.
  • Consider integrating other services using Docker Compose for a more complex application architecture.