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
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.
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.
Specify
localhost
and5432
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
- Checkout the sample-application using gorm with PGAdapter and Cloud Spanner.
- Learn more about gorm.
- Learn more about PGAdapter.
- File a GitHub issue to report a bug or ask a question about gorm with Spanner with PGAdapter.