Sling Academy
Home/Golang/Using Go + Docker Compose + PostgreSQL: An Example

Using Go + Docker Compose + PostgreSQL: An Example

Last updated: November 27, 2024

In this article, we are going to explore how to set up a Go application with Docker Compose alongside a PostgreSQL database. This setup is beneficial for many reasons, including ease of deployment and scalability.

Prerequisites

  • Basic understanding of Go programming language.
  • Docker and Docker Compose installed on your system.
  • PostgreSQL database understanding.

Project Structure

First, let’s create a directory for our project to keep everything organized.

mkdir go-docker-postgres
cd go-docker-postgres

Creating the Go Application

Create a new file called main.go and add the following Go code:

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/lib/pq"
)

func main() {
    connStr := "postgres://user:password@localhost/dbname?sslmode=disable"
    db, err := sql.Open("postgres", connStr)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
        panic(err)
    }

    fmt.Println("Successfully connected to the database!")
}

This Go application connects to a PostgreSQL database, where you will need to replace user, password, and dbname with your PostgreSQL credentials.

Creating Docker Environment

Create a Dockerfile for the Go application:

FROM golang:1.17-alpine
WORKDIR /app
COPY . .
RUN go mod init app && go mod tidy
RUN go build -o main .
CMD ["./main"]

Setting Up Docker Compose

Create a docker-compose.yml file:

version: '3.8'
services:
  db:
    image: postgres:13
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: password
      POSTGRES_DB: dbname
    ports:
      - "5432:5432"

  app:
    build: .
    depends_on:
      - db
    ports:
      - "8080:8080"

The above configuration includes two services: a PostgreSQL database and our Go application. Make sure that the environment variables in the database service match those in your Go connection string.

Running the Application with Docker Compose

In the terminal within your project directory, execute:

docker-compose up

This command will build the Go application, start the PostgreSQL service, and wire them together. You should see logs indicating that your application has successfully connected to the database.

Conclusion

This setup provides a starting point for developing Go applications with PostgreSQL in a Docker environment. You can further enhance this by adjusting configurations, adding more services, or integrating with other tools.

Next Article: Using Go + Docker Compose + MySQL: An Example

Previous Article: Optimizing Go Applications for Production Performance

Series: Development and Debugging in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant