Connecting your Cloud Spanner PostgreSQL databases with a JetBrains IDE such as IntelliJ IDEA will give you automatic code completion and error checking for your database code. You can also run queries directly from the IDE.
You can use the standard PostgreSQL JDBC driver to connect to Cloud Spanner PostgreSQL databases using PGAdapter. The PostgreSQL driver is already included in JetBrains products.
You need to start PGAdapter before connecting to Cloud Spanner. The quickest way to start it, is by using the pre-built Docker image. You can instruct IntelliJ or any other JetBrains IDE to start PGAdapter automatically at startup.
First create a run configuration for PGAdapter:
- Click on
Run | Edit configurations
and thenAdd new run configuration
. SelectDocker image
. - Fill in the following:
- Name:
PGAdapter
- Image ID or name:
gcr.io/cloud-spanner-pg-adapter/pgadapter
- Container name:
pgadapter-dev
- Bind port:
9030:5432
(or choose any other available local port that you want to use) - Command:
-c /credentials.json -x
- Bind mounts:
/local/path/to/credentials.json:/credentials.json:ro
(Change the local path to match your own credentials or a local service account file) - Run options:
--rm
(optional) - Pull image:
Always
(optional)
Then add the Run Configuration for PGAdapter as a startup task to IntelliJ:
- Click
IntelliJ | Preferences
. - Search for
Startup Tasks
. - Click
+
and add the PGAdapter Run Configuration.
Now you can create a connection to a Cloud Spanner database through PGAdapter using the standard
PostgreSQL driver. Open the Database tool window by clicking on the menu item
View | Tool Windows | Databases
and click on the New | Data Source | PostgreSQL
menu item in the window.
Fill in the following properties in the window:
- Host:
localhost
(assuming you are running PGAdapter on your local machine) - Port:
9030
(change if you started PGAdapter on a different port than 9030) - Authentication:
No auth
- Database:
projects/my-project/instances/my-instance/databases/my-database
- You now need to instruct IntelliJ to use the standard JDBC metadata methods to introspect the database. Click on the Advanced tab for this.
- Select the following option on the Advanced tab of the connection dialog:
- Click
Expert Options
in the bottom right corner of the dialog. - Select the option
Introspect using JDBC metadata
.
The latter option ensures that IntelliJ will use the standardised JDBC metadata methods for introspecting the selected database. Cloud Spanner does not support all pg_catalog tables that are available in normal PostgreSQL databases. PGAdapter automatically translates the standard pg_catalog queries that are needed for the JDBC metadata methods into queries that are supported by Cloud Spanner.
Click OK to create the database connection. You can now let IntelliJ introspect the database to find its tables and views.
Select the database connection that you just created in the Databases view and
click on the + | Query Console
menu option to open a new query console. You
can now execute SQL queries against on your Cloud Spanner database.
See the IntelliJ instructions for more details on how to work with query consoles.