Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,20 @@ class PGProxyRunner {
See [samples/java/jdbc](samples/java/jdbc) for a small sample application that adds
PGAdapter as a compile-time dependency and runs it together with the main application.

### Spanner PG Connector (`spgc`)

`spanner-pg-connector` (aliased as `spgc`) is a CLI tool designed to make working with existing
PostgreSQL command-line tools (such as `psql` or `pg_dump`) against Cloud Spanner
easier. It bundles PGAdapter and a minimal Java runtime—so you do not need Java or Docker installed
locally—and automatically starts and stops PGAdapter in the background when running your tool.

```shell
spgc psql -d "projects/my-project/instances/my-instance/databases/my-database"
```

See [spanner-pg-connector/README.md](spanner-pg-connector/README.md) for installation instructions,
environment variables, and more examples.

## Emulator
A pre-built Docker image that contains both PGAdapter and the Spanner Emulator can be started with
these commands:
Expand Down
85 changes: 85 additions & 0 deletions spanner-pg-connector/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Spanner PG Connector (`spgc`)

`spanner-pg-connector` (aliased as `spgc`) is a CLI tool designed to make working with existing
PostgreSQL command-line tools (such as `psql` or `pg_dump`) against Cloud Spanner
easier. It bundles PGAdapter and a minimal Java runtime—so you do not need Java or Docker installed
locally—and automatically starts PGAdapter in the background on a dynamically assigned localhost
port, configures the standard PostgreSQL environment variables (`PGHOST` and `PGPORT`), runs your
PostgreSQL tool against it, and stops PGAdapter when the tool exits.

The PostgreSQL client tool itself (for example `psql`) is not bundled and must be installed and
available on your `PATH`.

## Installation

### Linux (`x86_64`) and macOS (`aarch64` / `x86_64`)

```shell
curl -fsSL https://raw.githubusercontent.com/GoogleCloudPlatform/pgadapter/postgresql-dialect/spanner-pg-connector/install.sh | sh
```

### Windows (`x86_64`, PowerShell)

```powershell
irm https://raw.githubusercontent.com/GoogleCloudPlatform/pgadapter/postgresql-dialect/spanner-pg-connector/install.ps1 | iex
```

By default, the installer downloads the latest release into `~/.spanner-pg-connector` and adds it
to your `PATH`. You can customize the installation with `VERSION` and `INSTALL_DIR`:

<!--- {x-version-update-start:google-cloud-spanner-pgadapter:released} -->
```shell
curl -fsSL https://raw.githubusercontent.com/GoogleCloudPlatform/pgadapter/postgresql-dialect/spanner-pg-connector/install.sh \
| VERSION=v0.55.3 INSTALL_DIR="$HOME/.spanner-pg-connector" sh
```
<!--- {x-version-update-end} -->

### Manual Installation (Without the Installer Script)

You can also download and extract the release archive directly and add the extracted directory to
your `PATH` (both `spanner-pg-connector` and `spgc` are included in the archive):

<!--- {x-version-update-start:google-cloud-spanner-pgadapter:released} -->
```shell
VERSION=v0.55.3
PLATFORM=linux-x64 # linux-x64, mac-aarch64, or mac-x64
mkdir -p ~/.spanner-pg-connector
curl -fsSL "https://artifactregistry.googleapis.com/v1/projects/cloud-spanner-pg-adapter/locations/us/repositories/spanner-pg-connector/files/spanner-pg-connector:${VERSION}:spanner-pg-connector-${PLATFORM}.tar.gz:download?alt=media" \
| tar -xz -C ~/.spanner-pg-connector
export PATH="$HOME/.spanner-pg-connector:$PATH"
```
<!--- {x-version-update-end} -->

## Usage

Pass the client command and its arguments directly to `spgc` (or `spanner-pg-connector`):

```shell
spgc psql -d "projects/my-project/instances/my-instance/databases/my-database"
```

### Environment Variables

You can configure the project, instance, default database, or Cloud Spanner Emulator via
environment variables:

* `GOOGLE_CLOUD_PROJECT`: Google Cloud project ID.
* `SPANNER_INSTANCE`: Cloud Spanner instance ID.
* `SPANNER_DATABASE`: Default Cloud Spanner database ID (used if the command does not specify `-d` or `--dbname`).
* `SPANNER_EMULATOR_HOST`: If set, connects to the Cloud Spanner Emulator at `host:port` (for example `localhost:9010`) instead of Cloud Spanner. Must be unset when connecting to Cloud Spanner.

### Examples

```shell
export GOOGLE_CLOUD_PROJECT=my-project
export SPANNER_INSTANCE=my-instance

# Start an interactive psql session
spgc psql -d my-database

# Run a single query
spgc psql -d my-database -c "SELECT 1"

# Connect to the Cloud Spanner Emulator
SPANNER_EMULATOR_HOST=localhost:9010 spgc psql -d test-database
```
2 changes: 1 addition & 1 deletion spanner-pg-connector/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# PowerShell script to install Spanner PG Connector on Windows

$Project = if ($env:PROJECT_ID) { $env:PROJECT_ID } else { "cloud-spanner-pg-adapter" }
$Location = if ($env:AR_LOCATION) { $env:AR_LOCATION } else { "us-central1" }
$Location = if ($env:AR_LOCATION) { $env:AR_LOCATION } else { "us" }
$Repository = if ($env:AR_REPOSITORY) { $env:AR_REPOSITORY } else { "spanner-pg-connector" }
$Package = "spanner-pg-connector"
$Version = $env:VERSION
Expand Down
2 changes: 1 addition & 1 deletion spanner-pg-connector/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ set -e
# Configuration (Supports env overrides)
VERSION="${VERSION:-}"
PROJECT_ID="${PROJECT_ID:-cloud-spanner-pg-adapter}"
AR_LOCATION="${AR_LOCATION:-us-central1}"
AR_LOCATION="${AR_LOCATION:-us}"
AR_REPOSITORY="${AR_REPOSITORY:-spanner-pg-connector}"
INSTALL_DIR="${INSTALL_DIR:-${HOME}/.spanner-pg-connector}"

Expand Down
Loading