AWS

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
  _ "github.com/jackc/pgx/v5/stdlib"
)
func main() {
  //Use sql.Open to initialize a new sql.DB object
  //Pass the driver name and the connection string
  db, err := sql.Open("pgx", os.Getenv("PG_DSN"))
  if err != nil {
      fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
      os.Exit(1)
  }
  defer db.Close()
  //Call db.Ping() to check the connection
  pingErr := db.Ping()
  if pingErr != nil {
      log.Fatal(pingErr)
  }
  fmt.Println("Connected!")
}

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.
contact

Ready to get started?

Get in touch with our support team who can further advise

Get in touch
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.