本页介绍了如何将 PGAdapter 连接到 Spanner 模拟器。该模拟器在本地运行,您可以使用它来开发和测试应用,而无需创建 Google Cloud 项目或结算账号。由于模拟器仅将数据存储在内存中,因此所有状态(包括数据、架构和配置)都会在重启时丢失。模拟器提供与 Spanner 生产服务相同的 API,旨在用于本地开发和测试,而不是用于生产部署。
您可以通过以下三种不同的方式将 PGAdapter 连接到模拟器:
- 运行包含 PGAdapter 和模拟器的预构建合并 Docker 容器。
- 在本地机器上同时运行模拟器和 PGAdapter。
- 在 Docker 网络中同时运行模拟器和 PGAdapter。
组合 Docker 容器
运行同时包含 PGAdapter 和模拟器的预构建 Docker 映像。Docker 容器中的 PGAdapter 实例会自动配置为连接到容器中的模拟器。
使用以下命令启动 Docker 容器。
docker pull gcr.io/cloud-spanner-pg-adapter/pgadapter-emulator
docker run -d \
-p 5432:5432 \
-p 9010:9010 \
-p 9020:9020 \
gcr.io/cloud-spanner-pg-adapter/pgadapter-emulator
启动容器时,您无需指定项目、实例或数据库名称。默认情况下,它将使用项目名称 emulator-project
和实例名称 test-instance
。PGAdapter 会自动创建您连接到的任何数据库。
您可以使用以下命令连接到 PGAdapter 并使用 psql
执行语句。
psql -h localhost -p 5432 -d test-database
系统会自动创建数据库 test-database
。请执行以下语句,以验证您是否已连接到 PostgreSQL 方言数据库:
create table my_table (
id bigint not null primary key,
value varchar
);
insert into my_table (id, value) values (1, 'One');
select * from my_table;
您还可以直接连接到 Docker 容器内的模拟器,例如使用 gcloud CLI。
gcloud config configurations create emulator
gcloud config set auth/disable_credentials true
gcloud config set project emulator-project
gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
gcloud spanner instances list
gcloud spanner databases execute-sql test-database \
--instance=test-instance \
--sql="select * from my_table"
在本地机器上运行模拟器和 PGAdapter
您可以在本地机器上同时运行模拟器和 PGAdapter,并使用以下命令将 PGAdapter 连接到模拟器。
首先启动模拟器。
gcloud emulators spanner start
然后,启动 PGAdapter 并将其连接到模拟器。请注意,您必须在本地计算机上将 PGAdapter 作为 Java 应用启动,才能访问模拟器。如果您在 Docker 容器中启动 PGAdapter,它将无法访问在本地主机上运行的模拟器。
wget https://storage.googleapis.com/pgadapter-jar-releases/pgadapter.tar.gz \
&& tar -xzvf pgadapter.tar.gz
java -jar pgadapter.jar -p emulator-project -i test-instance \
-c "" \
-r autoConfigEmulator=true
PGAdapter 的其他命令行参数(用于连接到模拟器)如下所示:
-c ""
:这会指示 PGAdapter 不使用任何凭据。-r autoConfigEmulator=true
:这会指示 PGAdapter 连接到localhost:9010
,即默认的模拟器主机和端口。它还会指示 PGAdapter 自动创建用户连接到的任何数据库。这意味着,您无需先创建数据库,然后才能连接到该数据库。
您可以使用以下命令连接到 PGAdapter 并使用 psql
执行语句。
psql -h localhost -p 5432 -d test-database
系统会自动创建数据库 test-database
。请执行以下语句,以验证您是否已连接到 PostgreSQL 方言数据库:
create table my_table (
id bigint not null primary key,
value varchar
);
insert into my_table (id, value) values (1, 'One');
select * from my_table;
在 Docker 网络中运行模拟器和 PGAdapter
您可以在 Docker 网络中同时运行模拟器和 PGAdapter,并使用以下命令将 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 emulator-project"
- "-i test-instance"
- "-r autoConfigEmulator=true"
- "-e emulator:9010"
- "-c \"\""
- "-x"
ports:
- "5432:5432"
EOT
docker compose up -d
PGAdapter 和模拟器都在同一 Docker 网络中启动,并且 PGAdapter 配置为连接到模拟器。PGAdapter 的其他命令行参数(用于连接到模拟器)如下所示:
-c ""
:这会指示 PGAdapter 不使用任何凭据。-r autoConfigEmulator=true
:这会指示 PGAdapter 自动创建用户连接到的任何数据库。这意味着,您无需先创建数据库,然后再连接到该数据库。-e emulator:9010
:-e
用于指定 PGAdapter 应连接到的端点。emulator:9010
是同一 Docker 网络中模拟器的名称和端口号。-x
:这样,您就可以从本地机器连接到 PGAdapter。
您可以使用以下命令连接到 PGAdapter 并使用 psql
执行语句。
psql -h localhost -p 5432 -d test-database
系统会自动创建数据库 test-database
。请执行以下语句,以验证您是否已连接到 PostgreSQL 方言数据库:
create table my_table (
id bigint not null primary key,
value varchar
);
insert into my_table (id, value) values (1, 'One');
select * from my_table;
您还可以直接连接到 Docker 网络中的模拟器,例如使用 gcloud CLI。
gcloud config configurations create emulator
gcloud config set auth/disable_credentials true
gcloud config set project emulator-project
gcloud config set api_endpoint_overrides/spanner http://localhost:9020/
gcloud spanner instances list
gcloud spanner databases execute-sql test-database \
--instance=test-instance \
--sql="select * from my_table"