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.
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
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!")
}
<username>:<pw>@tcp(<HOST>:<port>)/<dbname>")
Environment variable example:
export MYSQL_DSN=admin:Mypassword@tcp(rds-mysql.xxxxx.amazonaws.com:3306)/test
See the previous section: MariaDB is a fork from MySQL, and as such, it works with the same DSN string format as MySQL.
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.
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!")
}
sqlserver://username:password@host:port?param1=value¶m2=value
Environment variable example:
export MSSQL_DSN=sqlserver://admin:Mypassword@rds-sqlserver.xxxxx.amazonaws.com:1433
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!")
}
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