Connect GoLang to Postgres on ObjectRocket
Introduction
If you’re developing applications using GoLang, you might want to interact with a PostgreSQL database from those applications. Fortunately, it just takes a few simple steps to install and configure the items you need to make this connection. In this article, we’ll show you how to connect GoLang to Postgres on ObjectRocket, providing detailed instructions for each step of the connection process.
Prerequisites
Before diving into this tutorial, take a moment to ensure that the following prerequisites are in place. You’ll need to install and configure the following for your operating system:
To check if Go is already installed and see the version number, use the command shown below:
1 | go version |
The output should look something like this:
1 | go version go1.13.4 windows/amd64 |
You’ll also need to create an instance of Postgres for your ObjectRocket account. To do this, use the ObjectRocket Mission Control panel.
Last but not least, you should have some basic knowledge of PostgreSQL in order to follow along with the instructions in this tutorial.
Installing the pq Golang Package
To create a connection between our Postgres database on ObjectRocket and GoLang, we need to install the Go PostgreSQL driver using the following command:
1 | go get -u github.com/lib/pq |
Connecting to Postgres Instance ObjectRocket
Since we already installed the GoLang third-party library that allows us to connect to Postgres from a Go script, let’s move on to the next step: connecting to a Postgres server instance.
Connecting to the Postgres Server Instance
To connect GoLang to our Postgres database, we need to supply the following details:
host
: This value can be the localhost or the URL of the server.port
: This value represents the port number of the server where it is listening for connections.database
: This value represents the name of the database to which we want to connect.username
andpassword
: These values represent the user credentials needed to connect to our server.
We can find the URL details for our server in the “CONNECT” tab located within the ObjectRocket Mission Control panel:
In our case, the host’s URL is: ingress.w98sujpz.launchpad.objectrocket.cloud.
After we confirm the details for our connection, we’ll create a file named main.go within our defined project directory.
We’ll add the following code to this file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package main import( "database/sql" "fmt" _ "github.com/lib/pq" ) const( host = "ingress.w98sujpz.launchpad.objectrocket.cloud" port = 4149 username = "pguser" password = "1234" database = "postgres" ) |
Lets take a closer look at what’s happening in this code:
First, we call
package main
, which instructs GoLang to compile this file as an executable file.Next, we import all the necessary dependencies.
We then create constants for our connection details using
const
.
Now that we understand the code we’ve used so far, we’ll add the rest of the script to the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | func main() { pg_con_string:= fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, username, password, database) db, err := sql.Open("postgres", pg_con_string) if err != nil{ panic(err) } defer db.Close() if err !=nil { panic(err) } defer db.Close() fmt.Println("You have successfully connected GoLang to ObjectRocket Postgres Server Instance") } |
Let’s pick up where we left off in the script and review what’s happening:
- In
func main()
, we build our connection string usingpg_con_string
, then we assign the necessary details to it.
NOTE: Although we included the sslmode
option, we set it to disable
as we are not going to use one.
Next, we open a GoLang connection to our Postgres database on ObjectRocket using the function
sql.open()
and passing the connection stringpg_con_string
.We then include some
if()
statements for error handling purposes.Finally, we print a notification in the console if the database connection was successful.
Test the code
At this point, we can test out our code. Let’s navigate to our file in the terminal and execute the following command:
1 | go build main.go |
The command shown above builds our Go application.
1 | go run main.go |
We can then run our application using the command above. If it works as expected, we should see something like this in the terminal:
Conclusion
If you’re planning to interact with a Postgres database from your GoLang applications, the first thing you’ll need to do is set up the proper connections. In this article, we walked you through this process, showing you how to connect to GoLang from Postgres on ObjectRocket. With these step-by-step instructions to guide you, you’ll be ready to connect to Postgres in your own GoLang applications.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started