[ Web Proxy ]
URL:
Viewing: https://cloud.google.com/spanner/docs/use-gorm [Back]  [Original]

Integrate Spanner with GORM (GoogleSQL dialect)  |  Google Cloud Documentation Skip to main content
Google Cloud Documentation [Google Cloud Documentation]
Send feedback

Integrate Spanner with GORM (GoogleSQL dialect) Stay organized with collections Save and categorize content based on your preferences.

GORM is an object-relational mapping tool for the Go programming language. It provides a framework for mapping an object-oriented domain model to a relational database.

You can integrate GoogleSQL-dialect databases with GORM using the open source Spanner Dialect (SpannerDialect).

Set up GORM with Spanner GoogleSQL-dialect databases

To use the GoogleSQL GORM dialect in your application, add the following code to the file where GORM is initialized:

import (
  "fmt"

  "gorm.io/gorm"
  _ "github.com/googleapis/go-sql-spanner"
  spannergorm "github.com/googleapis/go-gorm-spanner"
)

dsn := fmt.Sprintf("projects/%s/instances/%s/databases/%s", projectId, instanceId, databaseId)
db, err := gorm.Open(spannergorm.New(spannergorm.Config{DriverName: "spanner", DSN: dsn}), &gorm.Config{})

For more connection options for Spanner, see the GORM with GoogleSQL documentation.

Use GORM with Spanner GoogleSQL-dialect databases

For more information about the features and recommendations for using GORM with Spanner, consult the reference documentation on GitHub.

What's next

Send feedback

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-08-11 UTC.

Need to tell us more? [[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-08-11 UTC."],[],[]]

Web Proxy Viewer  |  New URL  |  Original Page