Mengintegrasikan Spanner dengan GORM (dialek PostgreSQL)
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
GORM adalah alat pemetaan relasional objek untuk bahasa pemrograman Go.
Framework ini menyediakan framework untuk memetakan model domain berorientasi objek ke database relasional.
Anda dapat mengintegrasikan database PostgreSQL Spanner dengan GORM menggunakan driver pgx PostgreSQL standar dan PGAdapter.
Menyiapkan GORM dengan database dialek PostgreSQL Spanner
Pastikan PGAdapter berjalan di mesin yang sama dengan
aplikasi yang terhubung menggunakan GORM dengan Spanner.
Tambahkan pernyataan impor untuk dialek GORM PostgreSQL
ke aplikasi Anda. Ini adalah driver yang sama seperti yang biasanya Anda gunakan dengan database PostgreSQL.
Tentukan localhost dan 5432 sebagai host dan port server database dalam
string koneksi GORM. GORM memerlukan
nama pengguna dan sandi dalam string koneksi. PGAdapter mengabaikannya.
Secara opsional, tentukan nomor port yang berbeda jika PGAdapter
dikonfigurasi untuk memproses port selain port PostgreSQL
default (5432).
PGAdapter tidak mendukung SSL. Secara default, GORM pertama-tama mencoba
terhubung dengan SSL yang diaktifkan. Menonaktifkan SSL dalam permintaan koneksi akan mempercepat
proses koneksi, karena memerlukan satu perjalanan bolak-balik lebih sedikit.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-09 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."]]