Connecting to the Cloud Spanner Emulator is supported with:
- PGAdapter version 0.26.0 and higher.
- Cloud Spanner Emulator 1.5.12 and higher.
The easiest way to use PGAdapter with the Cloud Spanner Emulator is to use the pre-built Docker image that contains both PGAdapter and the Emulator.
The pre-built Docker image contains both PGAdapter and the Spanner Emulator and can be started with these commands:
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
This Docker container configures PGAdapter to connect to a Cloud Spanner Emulator running inside the same container. You do not need to first create a Spanner instance or database on the Emulator before connecting to them. Instead, the instance and database are automatically created on the Emulator when you connect to PGAdapter.
You can also start PGAdapter and the Cloud Spanner Emulator separately, and then connect PGAdapter to the Cloud Spanner Emulator. The best way to do this is by following these steps:
- Start PGAdapter with the additional command line argument
-r autoConfigEmulator=true
. Adding this command line argument will instruct PGAdapter to automatically create the Cloud Spanner instance and database that is specified in any connection request. This means that you do not need to first create the instance and database before connecting to the emulator. It will also instruct PGAdapter to connect to the default emulator hostlocalhost:9010
. You can override this by starting PGAdapter with the-e
command line argument (see next point). - Start PGAdapter with the additional command line argument
-e <host:port>
with the host name and port number where the emulator is running. This is only necessary if the emulator is not running onlocalhost:9010
. This will typically be the case if both PGAdapter and the emulator are both running in separate Docker containers.
When running PGAdapter in a Docker container, PGAdapter must have network access to the Cloud Spanner Emulator. The easiest way to achieve this is by running both in Docker and connect them both to the same Docker network. This example uses Docker Compose to set up a Docker network and start both the emulator and PGAdapter.
cat <<EOT > docker-compose.yml
version: "3.9"
services:
emulator:
image: "gcr.io/cloud-spanner-emulator/emulator"
pull_policy: always
container_name: spanner-emulator
ports:
- "9010:9010"
- "9020:9020"
pgadapter:
depends_on:
emulator:
condition: service_started
image: "gcr.io/cloud-spanner-pg-adapter/pgadapter"
pull_policy: always
container_name: pgadapter-connected-to-emulator
command:
- "-p test-project"
- "-i test-instance"
- "-r autoConfigEmulator=true"
- "-e emulator:9010"
- "-c \"\""
- "-x"
ports:
- "5432:5432"
EOT
docker compose up -d
sleep 2
psql -h localhost -p 5432 -d test-database
Run docker compose down
to stop both the emulator and PGAdapter.
This script does the following:
- Pulls and starts the Cloud Spanner Emulator Docker image.
- Downloads the latest version of PGAdapter and unpacks this to the current directory.
- Starts PGAdapter and instructs it to automatically configure a connection to the emulator. Any
project, instance, or database that is specified in a connection string to PGAdapter will
automatically be created on the emulator. PGAdapter by default assumes that the emulator is
running on
localhost:9010
. - Sleep 2 seconds to wait for PGAdapter to start up. Then run
psql
against PGAdapter, which again connects to the emulator.
docker pull gcr.io/cloud-spanner-emulator/emulator
docker run -d -p 9010:9010 -p 9020:9020 gcr.io/cloud-spanner-emulator/emulator
wget https://storage.googleapis.com/pgadapter-jar-releases/pgadapter.tar.gz \
&& tar -xzvf pgadapter.tar.gz
java -jar pgadapter.jar -p test-project -i test-instance -c "" -r autoConfigEmulator=true > /dev/null 2>&1 &
sleep 2
psql -h localhost -p 5432 -d test-database
PGAdapter also respects the SPANNER_EMULATOR_HOST
environment variable. Setting this on the system
where PGAdapter runs will instruct PGAdapter to connect to the emulator specified in the variable.
Using this option requires that you manually create the instance and database that you want
PGAdapter to connect to before actually making the connection. The autoConfigEmulator=true
option
described above does this automatically for you.