Connect a Go application using GORM to PlanetScale
Spin up a PlanetScale MySQL database in seconds and connect to a Go application using GORM
In this tutorial, you'll learn how to connect a Go application to a PlanetScale MySQL database using a sample Go starter app with GORM.
Already have a Go application and just want to connect to PlanetScale? Check out the Go quick connect repo.
- A free PlanetScale account
- PlanetScale CLI — You can also follow this tutorial in the PlanetScale admin dashboard, but the CLI will make setup quicker.
This guide will integrate a simple Go (Golang) app with PlanetScale that will display a list of products stored in the database. If you have an existing application, you can also use that.
Clone the starter Go application:Terminalgit clone https://github.com/planetscale/golang-example.git
Enter into the folder:Terminalcd golang-example
.env:Terminalcp .env.example .env
Next, you need to set up your PlanetScale database and connect to it in the Go application.
You can create a database in the PlanetScale dashboard or from the PlanetScale CLI. This guide will use the CLI, but you can follow the database setup instructions in the PlanetScale quickstart guide if you prefer the dashboard.
Authenticate the CLI with the following command:Terminalpscale auth login
Create a new database with a default
mainbranch with the following command:Terminalpscale database create <DATABASE_NAME> --region <REGION_SLUG>
DATABASE_NAME, you can use any name with lowercase, alphanumeric characters, or underscores. You can also use dashes, but we don't recommend them, as they may need to be escaped in some instances.
REGION_SLUG, choose a region closest to you from the available regions or leave it blank.
That's it! Your database is ready to use. Next, let's connect it to the Go application and then add some data.
There are two ways to connect your Go app to PlanetScale:
- With an auto-generated username and password
- Using the PlanetScale proxy with the CLI
Both options are covered below.
Create a username and password with the PlanetScale CLI by running:Terminalpscale password create <DATABASE_NAME> <BRANCH_NAME> <PASSWORD_NAME>
A default branch,
main, is created when you create the database, so you can use that for
PASSWORD_NAMEvalue represents the name of the username and password being generated. You can have multiple credentials for a branch, so this gives you a way to categorize them. To manage your passwords in the dashboard, go to your database dashboard page, click "Settings", and then click "Passwords".
Take note of the values returned to you, as you won't be able to see this password again.
.envfile in your Go app and update
DSNas follows:TerminalDSN="<USERNAME>:<PASSWORD>@tcp(<ACCESS HOST URL>)/<DATABASE_NAME>?tls=true&interpolateParams=true"
ACCESS HOST URL, and
DATABASE_NAMEwith the appropriate values from the CLI output above. Do not remove the parentheses around the access host URL.
You can also get these exact values to copy/paste from your PlanetScale dashboard. In the dashboard, click on the database > "Connect" > "Connect with" language dropdown > "Go".
To connect with the PlanetScale proxy, you need the PlanetScale CLI.
Open a connection by running the following:Terminalpscale connect <DATABASE_NAME> <BRANCH_NAME>
If you're following this guide exactly and haven't created any branches, you can use the default branch,
A secure connection to your database will be established, and you'll see a local address you can use to connect to your application.
.envfile in your Go app and update it as follows:TerminalDSN="mysql://root@tcp(127.0.0.1:<PORT>)/<DATABASE_NAME>?interpolateParams=true"
The connection uses port
3306by default, but if that's being used, it will pick a random port. Make sure you paste in whatever port is returned in the terminal. Fill in the database name as well.
Now that you're connected let's add some data to see it in action. The sample application has an endpoint that you can use to run migrations to create your
products tables. It will seed your database with sample product and category data. You can find this in
Let's run those now.
First, start your Go app with:Terminalgo run .
Next, navigate to
localhost:8080/seedto run the migrations and the seeder.
You can now see the products and categories:
If you're using GORM in your Go application and do not want to use foreign key constraints, you can turn them off with this line in the
main.go file of the Go starter application:
If you prefer to use foreign key constraints in your Go application, you can skip the previous step. However, you need to first enable foreign key constraint support in your database settings page.
If you want to continue to play around with adding data on the fly, you have a few options:
- PlanetScale CLI shell
- PlanetScale dashboard console
- Your favorite MySQL client (for a list of tested MySQL clients, review our article on how to connect MySQL GUI applications)
The first two options are covered below.
You can use the PlanetScale CLI to open a MySQL shell to interact with your database.
You may need to install the MySQL command line client if you haven't already.
Run the following command in your terminal:Terminalpscale shell <DATABASE_NAME> <BRANCH_NAME>
This will open up a MySQL shell connected to the specified database and branch.Note
main, was automatically created when you created your database, so you can use that for
Add a record to the
productstable:SQLINSERT INTO `products` (name, description, image, category_id)VALUES ('Spaceship', 'Get ready for the trip of a lifetime', 'https://via.placeholder.com/300.png', 2);
idwill be filled with a default value.
You can verify it was added in the PlanetScale CLI MySQL shell with:SQLSELECT * FROM products;
exitto exit the shell.
You can now navigated the Go products page to see the new record.
If you don't care to install MySQL client or the PlanetScale CLI, another quick option is using the MySQL console built into the PlanetScale dashboard.
Go to your PlanetScale dashboard and select your Go database.
Click on "Console".
mainbranch and click "Connect".
Add a new record to the
producttable with:SQLINSERT INTO `products` (name, description, image, category_id)VALUES ('Spaceship', 'Get ready for the trip of a lifetime', 'https://via.placeholder.com/300.png', 2);
You can confirm that it was added by running:SQLSELECT * FROM products;
You can now refresh the Go products page to see the new record.
Once you're done with initial development, you can enable safe migrations on your
main production branch to protect it against direct schema changes and enable zero-downtime schema migrations.
When you're reading to make more schema changes, you'll create a new branch off of your production branch. Branching your database creates an isolated copy of your production schema so that you can easily test schema changes in development. Once you're happy with the changes, you'll open a deploy request. This will generate a diff showing the changes that will be deployed, making it easy for your team to review.
Learn more about how PlanetScale allows you to make non-blocking schema changes to your database tables without locking or causing downtime for production databases.