Integrate Spanner with gorm ORM

Stay organized with collections Save and categorize content based on your preferences.

You can use the gorm object-relational manager (ORM) in combination with the standard PostgreSQL pgx driver and PGAdapter.

Set up gorm with Spanner PostgreSQL-dialect databases

  1. Ensure that PGAdapter is running on the same machine as the application that is connecting using gorm with Spanner.

    For more information, see Start PGAdapter.

  2. Add an import statement for the PostgreSQL gorm dialect to your application. This is the same driver as you would normally use with a PostgreSQL database.

  3. Specify localhost and 5432 as the database server host and port in the gorm connection string. gorm requires a username and password in the connection string. PGAdapter ignores these.

    • Optionally specify a different port number if PGAdapter is configured to listen on a port other than the default PostgreSQL port (5432).
    • PGAdapter does not support SSL. gorm by default first tries to connect with SSL enabled. Disabling SSL in the connection request speeds up the connection process, because it requires one fewer round trip.
    import (
      "gorm.io/driver/postgres"
      "gorm.io/gorm"
    )
    
    dsn := "host=localhost user=gorm password=gorm dbname=gorm port=5432 sslmode=disable"
    db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
    

See the gorm with PostgreSQL documentation for more connection options for PostgreSQL.

Use gorm with Spanner PostgreSQL-dialect databases

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

What's next