Skip to main content

Go

This guide will walk you through the process of connecting to your Rapidapp PostgreSQL database using Go. By the end of this tutorial, you'll be able to establish a connection to your database and start interacting with your data programmatically.

Prerequisites

Before you begin, ensure that you have the following:

  • Go installed on your system. You can download it from the official Go website.
  • A code editor or IDE like Visual Studio Code, GoLand, or Vim.
  • PostgreSQL database credentials from Rapidapp:
    • Hostname
    • Port
    • Database
    • Username
    • Password

Step 1: Set Up Your Go Project

  • Create a new directory for your project and navigate into it:
mkdir rapidapp-postgres-connection
cd rapidapp-postgres-connection
  • Initialize a Go module:
go mod init rapidapp-postgres-connection
  • Install the pq PostgreSQL driver:
go get -u github.com/lib/pq

Step 2: Write Go Code to Connect to PostgreSQL Using DSN

Create a new file, main.go, and add the following code:

package main

import (
"database/sql"
"fmt"
"log"

_ "github.com/lib/pq"
)

func main() {
// Replace these with your actual Rapidapp PostgreSQL credentials
host := "your-rapidapp-hostname"
port := 5432 // Default PostgreSQL port
user := "your-username"
password := "your-password"
dbname := "your-database"

// Use DSN (Data Source Name) format to create the connection string
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=require",
host, port, user, password, dbname)

// Open the connection to the database
db, err := sql.Open("postgres", psqlInfo)
if err != nil {
log.Fatalf("Failed to connect to the database: %v", err)
}
defer db.Close()

// Test the connection
err = db.Ping()
if err != nil {
log.Fatalf("Unable to ping the database: %v", err)
}

fmt.Println("Successfully connected to Rapidapp PostgreSQL database!")
}

Step 3: Replace Placeholder Values

Replace the placeholder values in the host, port, user, password, and dbname variables with your actual Rapidapp PostgreSQL database credentials:

  • host: The hostname of your PostgreSQL server.
  • port: The port number (default is 5432).
  • user: Your database username.
  • password: Your database password.
  • dbname: The name of your database.

Example:

host := "pg.rapidapp.io"
port := 5432
user := "myusername"
password := "mypassword"
dbname := "mydatabase"

Step 4: Run Your Application

  • Compile and run your Go application:
go run main.go
  • If everything is set up correctly, you should see the message:
Successfully connected to Rapidapp PostgreSQL database!

If there’s an issue, an error message will be printed to help you diagnose the problem.

Conclusion

Congratulations! You’ve successfully connected to your Rapidapp PostgreSQL database using Go with DSN notation. Now, you're ready to start executing SQL queries, performing CRUD operations, and integrating your Go application with the database.

For more advanced usage and examples, refer to the Rapidapp documentation here.

Happy coding! 🚀