Integrate Spanner with GORM (PostgreSQL dialect)

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 Spanner PostgreSQL databases with GORM using 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 theapplication that is connecting using GORM with Spanner.

    For more information, seeStart PGAdapter.

  2. Add an import statement for the PostgreSQL GORM dialectto your application. This is the same driver as you would normally use with aPostgreSQL database.

  3. Specifylocalhost and5432 as the database server host and port in theGORM connection string. GORM requires ausername and password in the connection string. PGAdapter ignores these.

    • Optionally, specify a different port number if PGAdapter isconfigured to listen on a port other than the default PostgreSQLport (5432).
    • PGAdapter does not support SSL. GORM by default first tries toconnect with SSL enabled. Disabling SSL in the connection request speedsup 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 theGORM with PostgreSQL documentationfor more connection options for PostgreSQL.

Use GORM with Spanner PostgreSQL-dialect databases

For more information about the features and recommendationsfor using GORM with Spanner, consult thereference documentationon GitHub.

What's next

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

Last updated 2026-02-19 UTC.