How to set up a database locally?

As a Frontend developer, I don’t like to have a mess in my work environment. Sometimes I have to install some services in my local environment to work with the project. And when I finish my tasks, those services don’t disappear. I hate it. So I found a solution for some services. In this post, I will show You how I work with databases. It’s really simple.

Add database with Docker to your project

I’m currently using the Postgresql database, so in this example, I will use this database to show you how easy it is. You can use other database engines as well. I will use Docker compose so this code can be easily added to your git repository and shared between different users.

Prerequisities:

Docker compose file

First of all we have to create a docker-compose file. Docker compose allows me to write instructions for Docker how to build images.

Go to your project directory and create a docker-compose.yaml file. I made a new directory in my project for this, and I called it db. You can do it just like me, or you can choose your own way. Below I’m showing you how my docker-compose look like.

services:
	db:
		build
			context:./
			dockerfile: Dockerfile
		ports:
			- 5432:5432
		environment:
			- POSTGRES_USER=moonrise
			- POSTGRES_PASSWORD=moonrise
		volumes:
			- db-data:/path/to/your/dir
volumes:
	db-data:

Let’s explain this file a little bit. First, we are creating a service called db. I know. It’s a super creative name. In that db service, you can see 4 main keys: build, ports, environment, and volumes. The build section includes all information about where to find the necessary information to build an image. Next, you can see the ports section. Here I added the port which will be exposed from the container. Next, I’m adding some environment variables for this image. In this case, I provided a username and password for my database. The last section is volumes. I put here a path to the local directory where all data will be stored. If I will delete the container and create a new one but this directory will have some files then these files will be used to recreate the state of the previous database.

Dockerfile – next big thing

Next step on this road is creating a Docker file. Below you have my docker file from current project.

FROM postgres

ADD init.sql /docker-entrypoint-initdb.d

It’s really simple. But I want to explain it quickly. In first-line I specified what official image should be used to create my database. Word postgres means that Docker will take the latest Postgres image. If you want to use other Postgres images you can find them in Docker Hub.

Third line is responsible for running file init.sql which will setup our database.

Let’s now create this file. It is important to create it in the same directory with Dockerfile. And here is my init.sql file:

CREATE DATABASE moon

Only one line of code. It will create a database in container. This database will be called moon. You can use your own name.

Terminal command

The last step is running your database. Open terminal n the root directory, where you have your docker-compose.yaml file and type:

docker-compose up -d

It will start your container in detached mode (in the background).

Now you can connect to your database from your application or using another tool for connecting with databases. I’m using Table Plus for Mac. It’s a really good GUI for managing relational databases. But you have to pay for it.

The end

That’s all. You now know how to setup database for your local development environment. I hope it will be helpful for you. Thanks for your time and see you in next post.