Building golang project from scratch - part 3 - setting up the database using docker
Tags: docker, database, go, beginners
Published: 2023-12-29 10:07:36


## For those who prefer the video format

https://www.youtube.com/watch?v=kww6LL7OWdY

## Explanation

Setting up your own postgres database using docker container
Instead of installing the postgres database on your system, you can use docker container to initiate a database for quick tests

```yaml
version: '3.8'

services:
  postgres:
    image: postgres:latest
    container_name: my_db_pg_container
    restart: always
    environment:
      POSTGRES_DB: my_db
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - "5432:5432"
    volumes:
      - ./migrations/1_db_init.up.sql:/docker-entrypoint-initdb.d/init.sql
    networks:
      - my_network

networks:
  my_network:
```

Your Docker Compose file sets up a PostgreSQL database using the official PostgreSQL Docker image. It also includes a volume mount for initializing the database with an SQL script and uses a custom network. Here’s a breakdown of the key components:

1. Docker Compose Version:
`version: ‘3.8’`: Specifies the version of the Docker Compose file syntax.

2. Services:
— `postgres`: Defines a service named “postgres.”

- `image: postgres:latest`: Specifies the PostgreSQL Docker image to use. `latest` refers to the latest version of the image.

— `container_name: my_db_pg_container`: Assigns a custom name to the PostgreSQL container.

— `restart: always`: Configures the container to restart automatically if it stops.

— `environment`: Sets environment variables for the PostgreSQL container.

— `POSTGRES_DB: my_db`: Specifies the name of the initial database to be created.

— `POSTGRES_USER: postgres`: Sets the default user for the PostgreSQL database.

— `POSTGRES_PASSWORD: postgres`: Sets the password for the default user.

— `ports`: Maps the host machine’s port 5432 to the container’s port 5432, allowing access to the PostgreSQL database.

— `volumes`: Mounts the `./migrations/1_db_init.up.sql` file into the `/docker-entrypoint-initdb.d/init.sql` path inside the container. This file is executed during the initialization of the database.

— `networks`: Connects the service to a custom network named “my_network.”

3. Networks:
— `my_network`: Defines a custom bridge network named “my_network” to allow communication between containers.

This configuration is useful for setting up a PostgreSQL database with a predefined schema or initial data using the SQL script (`1_db_init.up.sql`). When the container starts, it will execute the script, initializing the database with the specified settings.

### To use this Docker Compose file

1. Save the file with a `.yml` or `.yaml` extension (e.g., `docker-compose.yml`).
2. Open a terminal in the directory containing the file.
3. Run the following command to start the PostgreSQL container:

```bash
    docker-compose up -d
```

This will download the PostgreSQL image (if not already present) and start the container with the specified configuration. The PostgreSQL server will be accessible on `localhost:5432`, and you can connect to it using the provided credentials.

To stop the container, use:

```bash
    docker-compose down
```

Adjust the configuration based on your specific requirements and security considerations.

## Conclusion

In this article we learnt how we can quickly spin up a database using docker compose

Happy coding!

## Subscribe to my Youtube channel

Subscribe to my youtube channel if you are on the lookout for more such awesome content in video format.

>    https://www.youtube.com/@dsysd-dev

## Claps Please!

If you found this article helpful I would appreciate some claps 👏👏👏👏, it motivates me to write more such useful articles in the future.

Follow me on Medium for regular awesome content and insights.
Subscribe to my Newsletter

If you like my content, then consider subscribing to my free newsletter, to get exclusive, educational, technical, interesting and career related content directly delivered to your inbox

>    https://dsysd.beehiiv.com/subscribe

## Important Links

Thanks for reading the post, be sure to follow the links below for even more awesome content in the future.

twitter: https://twitter.com/dsysd_dev
youtube: https://www.youtube.com/@dsysd-dev
github: https://github.com/dsysd-dev
medium: https://medium.com/@dsysd-dev
email: dsysd.mail@gmail.com
linkedin: https://www.linkedin.com/in/dsysd-dev/
newsletter: https://dsysd.beehiiv.com/subscribe
gumroad: https://dsysd.gumroad.com/
Dev.to: https://dev.to/dsysd_dev/
Instagram: https://www.instagram.com/dsysd.dev/