How I Seed My Database With Go

Redha Juanda
Sep 13 · 3 min read
Golang Database Seeder
Golang Database Seeder

The thing I miss most about Laravel or any other frameworks in other programming languages ​​is the ease of migrating and seeding databases.

I’ve tried to find a package for database seeder in Go, I did find one, but it seems that this package is not really what I needed, because with polluter I will need to define all the data manually in the .yaml file.

I needed a more flexible seeder, where I can generate random data, execute SQL files, or maybe also call an existing Usecase/Repository. So in this article, I will try to explain how I achieved that.

First, we need to create a table in the database, let’s say the table name is customers. The following is the SQL script to generate the table:

Then create a directory called seeds and a new file called seeder.go

mkdir seeds && cd seeds && touch seeder.go

And define a Seed struct that holds the database which we will use for seeding later.

I use the standard database/sql package for this article, you can change it with your preferred package tho, maybe like ozzo-dbx.

Then we create a new file called customers.go and define a method on Seed struct called CustomerSeed, this method is where we implement our database insertion, take a look at the following code:

In the CustomerSeed method I generated 100 customers data with the faker package, this package is used for generating fake data such as names, addresses, and phone numbers.

Then go back to the seeder.go file, and add a function seed with the following code:

In this function, we receive the Seed struct as parameter and the name of the function/method to be executed. You can see here we’re taking advantage of reflect package to execute the specific method (in this case we want to execute our seeder methods) based on the method name. If you’re still with me, the idea is we want to be able to pass the seeder method name as an argument when we run the program. With reflect we can reflect the method variable, and use reflect.Call to call it.

And then still in the seeder.go file, we will add one more function Execute that accepts 2 parameters, the database and the name of the seeder method (optional).

If no function/method name passed, we also use reflect here to iterate over all methods on Seed struct, so that it will execute all of our seeder methods.

Now our seeder.go file should look like this:

Now create a main.go file with the following code:

In the main file, we receive one argument seed followed by names of the seeder method that we want to execute, if no seeder method name is given, all seeders will be executed.

Now we can run our seeder with the command:

go run main.go seed

or only execute a specific seeder method

go run main.go seed CustomerSeed

or if you prefer to build it first

go build -o app main.go &&
./app seed CustomerSeed

And if you want to add new seed, you only need to add another method on Seed struct, let’s say ProductSeed

func(s Seed) ProductSeed() {   // insert product here

You also can run the seeder with multiple arguments:

go run main.go seed CustomerSeed ProductSeed

The implementation of the seeder method is more flexible, you can call an existing repository, or even execute an SQL file as it is just the regular Go method/function.

I will demonstrate how we execute the SQL file in the seeder method. First, create a new products table with the following script:

And create another directory and SQL file in the seeds directory

cd seeds && 
mkdir scripts &&
cd scripts &&
touch products.sql

With the following scripts:

Now create a new seeder method inproducts.go

Run go run main.go seed ProductSeed and it will seed the product data from the SQL file.

You can find all the codes here on my Github.

Hope this helps! 😉
Also if you have a better idea, feel free to leave a comment.


Easy read, easy understanding.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store