Database Setup

Configuring Your PostgreSQL Database.

Once you've completed your prerequisites, it's time to configure your database on your local machine.

PostgreSQL

For this tutorial, I will be using Postgres as my preferred database. Postgres has become the dominant open source database with well maintained drivers for Go.

When developing and deploying an application, you want to make sure all software and configuration matches expectations and can be reproduced. Containers solve this problem for us. I will use Docker Desktop to spin up a container.

Let's use Docker to get a postgres-enabled image for our container. This saves us a ton of work: we don't have to download/install/configure a local postgres server.

Open Terminal (⌥Opt F12), and type the following command.

docker run --name bookstore -p 5432:5432 -e POSTGRES_PASSWORD=******** -d postgres

Or you can directly search for the "postgres" image in Docker Desktop.

docker1

Once the database is up and running, you will see it appearing in the list of containers.

docker2

The postgres container runs on default port 5432. Next, get inside the container and create a new db.

create_db_0

create_db_1

Trying with GoLand

Starting a Postgres container is also quite straightforward with GoLand.

Access your project in GoLand and select the three dots located on your left, then click Services.

goland_1

You'll notice Docker appearing. You can also explore all the containers, images, volumes, etc.

goland_2

Click Images and then provide the image name to pull from docker registry.

goland_3

After the image has been successfully pulled, right-click on the "postgres" image and select "Create Container".

goland_4

Under Modify options provide bind ports and environment variables.

goland_5

goland_6

goland_7

goland_8

Once you have supplied all the necessary information, click Run.

goland_9

You can see the container is up and running.

goland_10

One of the standout features is the ability to visualize databases seamlessly in GoLand. Ensure to activate the Database Tools and SQL plugin for this functionality.

db1

Click + and add a new Data Source --> PostgreSQL.

db2

Provide the necessary database credentials like database username, password, host etc.

Click Apply and then OK

db3

Within the console, you have the capability to execute your SQL queries. Here, I will demonstrate creating a new database.

db4

We've successfully configured our database. Next, our attention will turn to setting up the new project and installing necessary dependencies.