Nov 13, 2025

Connect to AWS RDS with Go

In this article we show you how to connect to different AWS RDS databases using Golang

Connect to AWS RDS with Go

In this article, we provide examples of connecting to the six major databases on RDS with native authentication. Native auth is the default way of authenticating users against a database based on the submitted username and password.

Connect to AWS RDS PostgreSQL 

  • Create a PostgreSQL database on RDS
  • Crease a main.go file and insert the following code
package main

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

	// We are using the pgx driver to connect to PostgreSQL.
	// The underscore alias is used because we only need the package's side effects (its registration with the sql package).
	_ "github.com/jackc/pgx/v5/stdlib"
)

func main() {
	// Initialize a new sql.DB object using sql.Open.
	// The first argument is the driver name ("pgx"), and the second is the DSN (Data Source Name)
	// retrieved from the environment variable PG_DSN.
	db, err := sql.Open("pgx", os.Getenv("PG_DSN"))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Unable to open database connection: %v\n", err)
		os.Exit(1)
	}

	// db.Close() is deferred to ensure the connection is closed when the main function exits.
	defer db.Close()

	// Call db.Ping() to verify that the database connection is alive and establishable.
	pingErr := db.Ping()
	if pingErr != nil {
		// log.Fatal prints the error and then calls os.Exit(1).
		log.Fatal(pingErr)
	}

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

os.Getenv retrieves the value of the environment variable named by that key. To set the environment variable, create .envrc file in the same directory as your main.go file and add the variable in the following format:

postgresql://[user[:password]@][netloc][:port][/dbname][?param1=value1&...]

Environment variable example:

export PG_DSN=postgres://postgres:mypassword@rds-postgres.xxxxx.amazonaws.com:5432
  • Load the new environment variable into your current shell by running the source .envrc  command. 
  • Run go run main.go to test your connection.

Connect to AWS RDS MySQL 

  • Create a MySQL database on RDS
  • Crease a main.go file and insert the following code:
package main

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

	// Use the go-sql-driver to connect to MySQL
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	// Use sql.Open to initialize a new sql.DB object
	// Pass the driver name and the connection string
	db, err := sql.Open("mysql", os.Getenv("MYSQL_DSN"))
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Call db.Ping() to check the connection
	pingErr := db.Ping()
	if pingErr != nil {
		log.Fatal(pingErr)
	}
	fmt.Println("Connected!")
}

  • Create a DSN string in the following format:
<username>:<pw>@tcp(<HOST>:<port>)/<dbname>")

Environment variable example:

export MYSQL_DSN=admin:Mypassword@tcp(rds-mysql.xxxxx.amazonaws.com:3306)/test
  • Load the new environment variable into your current shell by running the source .envrc command. 
  • Run go run main.go to test your connection.

Connect to AWS RDS MariaDB 

See the previous section: MariaDB is a fork from MySQL, and as such, it works with the same DSN string format as MySQL.

Connect to AWS RDS Aurora 

In RDS, you can choose between Amazon Aurora MySQL or PostgreSQL-Compatible Edition. Depending on the edition, you can use the respective system’s DSN string described earlier in this article.

Connect to AWS RDS SQL Server 

  • Create a SQL Server database on RDS
  • Crease a main.go file and insert the following code:
package main

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

	// Use the go-mssqldb driver to connect to SQL Sever
	_ "github.com/denisenkom/go-mssqldb"
)

func main() {
	// Use sql.Open to initialize a new sql.DB object
	// Pass the driver name and the connection string
	db, err := sql.Open("sqlserver", os.Getenv("MSSQL_DSN"))
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Call db.Ping() to check the connection
	pingErr := db.Ping()
	if pingErr != nil {
		log.Fatal(pingErr)
	}
	fmt.Println("Connected!")
}

  • Create a DSN string in the following format:
sqlserver://username:password@host:port?param1=value&param2=value

Environment variable example:

export MSSQL_DSN=sqlserver://admin:Mypassword@rds-sqlserver.xxxxx.amazonaws.com:1433
  • Load the new environment variable into your current shell by running the source .envrc command. 
  • Run go run main.go to test your connection.

Connect to AWS RDS Oracle

  • Create a Oracle database on RDS
  • Crease a main.go file and insert the following code
package main

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

	// Use the go-ora driver to connect to Oracle
	_ "github.com/sijms/go-ora/v2"
)

func main() {
	// Use sql.Open to initialize a new sql.DB object
	// Pass the driver name and the connection string
	db, err := sql.Open("oracle", os.Getenv("ORACLE_DSN"))
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	// Call db.Ping() to check the connection
	pingErr := db.Ping()
	if pingErr != nil {
		log.Fatal(pingErr)
	}
	fmt.Println("Connected!")
}

  • Create a DSN connection string in the following format:
oracle://user:pass@server/service_name

Environment variable example:

export ORACLE_DSN=oracle://admin:Mypassword@rds-oracle.xxxxx.amazonaws.com:1521/TEST

Note: service_name = DB name in RDS console

  • Load the new environment variable into your current shell by running the source .envrc command. 
  • Run go run main.go to test your connection.

Do you need help getting started?

Our team of professional data engineers is here to help you get the most out of your data pipeline. Reach out today.

By clicking “Accept All Cookies”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.