Search code examples
google-cloud-spannergoogle-cloud-spanner-emulator

connecting Spanner Emulator through PGAdapter


Is there a way to connect to the Spanner Emulator through a PGAdapter? I want to develop an application with PostgreSQL Dialect database.


Solution

  • EDIT 2024-01-17:

    The Cloud Spanner Emulator supports the PostgreSQL dialect, and PGAdapter can also connect to the emulator. The simplest way to use PGAdapter with the emulator is to use the combined Docker image that contains both PGAdapter and the Emulator, and that has PGAdapter configured to connect to the emulator, and to automatically create any instance or database that is referenced in any incoming connection request. The latter makes it very easy to use for testing, as you don't have to worry about creating an instance and a database first.

    Use it like this:

    docker pull gcr.io/cloud-spanner-pg-adapter/pgadapter-emulator
    docker run \
      -d -p 5432:5432 \
      gcr.io/cloud-spanner-pg-adapter/pgadapter-emulator
    sleep 2
    psql -h localhost -p 5432 -d test-database
    

    More information can be found here: https://github.com/GoogleCloudPlatform/pgadapter?tab=readme-ov-file#emulator


    OLD ANSWER:

    The Cloud Spanner emulator does not yet support the PostgreSQL dialect. PGAdapter can currently only be used with PostgreSQL-dialect Cloud Spanner databases, which means that even though you can connect to the emulator using PGAdapter, it would refuse to connect to any database on that emulator, because they use the GoogleSQL dialect. For the time being, the recommendation is to use either one of the following options for development instances:

    1. A free trial instance of Cloud Spanner: https://cloud.google.com/spanner/docs/free-trial-instance
    2. A fractional Cloud Spanner instance: https://cloud.google.com/spanner/docs/instances#compute-capacity There is also a GitHub issue for this here: https://github.com/GoogleCloudPlatform/pgadapter/issues/793 I recommend that you follow that issue as well. I'll update it once making a connection is actually possible.