Accessing relational databases

Using Go, you can incorporate a wide variety of databases and data access approaches into your applications. Topics in this section describe how to use the standard library’s database/sql package to access relational databases.

For an introductory tutorial to data access with Go, please see Tutorial: Accessing a relational database.

Go supports other data access technologies as well, including ORM libraries for higher-level access to relational databases, and also non-relational NoSQL data stores.

Supported database management systems

Go supports all of the most common relational database management systems, including MySQL, Oracle, Postgres, SQL Server, SQLite, and more.

You’ll find a complete list of drivers at the SQLDrivers page.

Functions to execute queries or make database changes

The database/sql package includes functions specifically designed for the kind of database operation you’re executing. For example, while you can use Query or QueryRow to execute queries, QueryRow is designed for the case when you’re expecting only a single row, omitting the overhead of returning an sql.Rows that includes only one row. You can use the Exec function to make database changes with SQL statements such as INSERT, UPDATE, or DELETE.

For more, see the following:

Transactions

Through sql.Tx, you can write code to execute database operations in a transaction. In a transaction, multiple operations can be performed together and conclude with a final commit, to apply all the changes in one atomic step, or a rollback, to discard them.

For more about transactions, see Executing transactions.

Query cancellation

You can use context.Context when you want the ability to cancel a database operation, such as when the client’s connection closes or the operation runs longer than you want it to.

For any database operation, you can use a database/sql package function that takes Context as an argument. Using the Context, you can specify a timeout or deadline for the operation. You can also use the Context to propagate a cancellation request through your application to the function executing an SQL statement, ensuring that resources are freed up if they’re no longer needed.

For more, see Canceling in-progress operations.

Managed connection pool

When you use the sql.DB database handle, you’re connecting with a built-in connection pool that creates and disposes of connections according to your code’s needs. A handle through sql.DB is the most common way to do database access with Go. For more, see Opening a database handle.

The database/sql package manages the connection pool for you. However, for more advanced needs, you can set connection pool properties as described in Setting connection pool properties.

For those operations in which you need a single reserved connection, the database/sql package provides sql.Conn. Conn is especially useful when a transaction with sql.Tx would be a poor choice.

For example, your code might need to:

For more, see Using dedicated connections.