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 Spanner PostgreSQL databases with GORM using 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.
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 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.
[[["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 2025-08-28 UTC."],[],[],null,["# Integrate Spanner with GORM (PostgreSQL dialect)\n\nGORM is an object-relational mapping tool for the Go programming language.\nIt provides a framework for mapping an object-oriented domain model to a relational database.\n\nYou can integrate Spanner PostgreSQL databases with GORM using the standard PostgreSQL pgx driver and PGAdapter.\n\nSet up GORM with Spanner PostgreSQL-dialect databases\n-----------------------------------------------------\n\n1. Ensure that PGAdapter is running on the same machine as the\n application that is connecting using GORM with Spanner.\n\n For more information, see [Start PGAdapter](/spanner/docs/pgadapter-start).\n2. Add an import statement for the PostgreSQL GORM dialect\n to your application. This is the same driver as you would normally use with a\n PostgreSQL database.\n\n3. Specify `localhost` and `5432` as the database server host and port in the\n GORM connection string. GORM requires a\n username and password in the connection string. PGAdapter ignores these.\n\n - Optionally, specify a different port number if PGAdapter is configured to listen on a port other than the default PostgreSQL port (5432).\n - 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.\n\n import (\n \"gorm.io/driver/postgres\"\n \"gorm.io/gorm\"\n )\n\n dsn := \"host=localhost user=gorm password=gorm dbname=gorm port=5432 sslmode=disable\"\n db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})\n\nSee the [GORM with PostgreSQL documentation](https://gorm.io/docs/connecting_to_the_database.html#PostgreSQL)\nfor more connection options for PostgreSQL.\n\nUse GORM with Spanner PostgreSQL-dialect databases\n--------------------------------------------------\n\nFor more information about the features and recommendations\nfor using GORM with Spanner, consult the\n[reference documentation](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/golang/gorm)\non GitHub.\n\nWhat's next\n-----------\n\n- Checkout the [sample application](https://github.com/GoogleCloudPlatform/pgadapter/blob/-/samples/golang/gorm/sample.go) using GORM with PGAdapter and Spanner.\n- Learn more about [GORM](https://gorm.io/).\n- Learn more about [PGAdapter](/spanner/docs/pgadapter).\n- [File a GitHub issue](https://github.com/GoogleCloudPlatform/pgadapter/issues) to report a bug or ask a question about using GORM with Spanner with PGAdapter."]]