Use the SQL Connect emulator for CI/CD

Firebase SQL Connect provides you with a local emulator for end-to-end prototyping as well as continuous integration and continuous deployment (CI/CD) flows:

  • The SQL Connect emulator interacts with a local integrated PGLite database instance to let you prototype queries and mutations and test client code in a fully local environment.
  • The SQL Connect emulator can also be used for non-interactive work. It lets you run automated tests and is suitable for use with CI/CD workflows. This is useful when your schemas are stable and you want to prototype and test client-side code.

This guide covers installation and usage of the emulator in more detail than the quickstart.

Install the SQL Connect emulator

Before installing the Local Emulator Suite to use the SQL Connect emulator, you will need:

  • Node.js version 18.0 or higher.

Install the Firebase CLI and set up project directory

  1. Install the Firebase CLI, following the installation guide. Be sure to update regularly, since the SQL Connect emulator is under active development with bug fixes and new features.

  2. If you haven't already done so, initialize the current working directory as a Firebase project, following prompts to specify which products to use:

    firebase init

Set or modify the Local Emulator Suite configuration

If you started the SQL Connect emulator from the SQL Connect VS Code extension, the emulator was installed for you, if needed.

You can use the Firebase CLI to manually install the emulator along with other selected components of the Local Emulator Suite. This command starts a configuration wizard that lets you select emulators of interest, download the corresponding emulator binary files, and set emulator ports if the defaults are not appropriate.

  firebase init emulators

Once an emulator is installed, no update checks are performed and no additional automatic downloads will occur until you update your Firebase CLI version.

Choose a Firebase project

In the setup flow, the Firebase CLI prompts you to choose or create a Firebase project. If you choose an existing project that you've already set up with SQL Connect, the configuration you chose during project setup will be suggested.

Set up the emulator

Configure the emulator

Running the firebase init flow will guide you through emulator setup options. Like other emulators in the Local Emulator Suite, configuration parameters are stored in local project files.

  • Your firebase.json file contains emulator port assignments.
    • The emulators:ui key does not apply to the SQL Connect emulator.

Work with local and production SQL Connect resources

If you want to be sure not to impact production resources, set a demo- projectID or make sure your client code is instrumented to connect to the emulator, as discussed in a later section.

Start the emulator

If you're running the emulator non-interactively, for example for CI/CD workflows, start it with the exec option.

firebase emulators:exec ./path/to/test-script.sh

If you're integrating predefined queries and mutations in client code and are using the emulator specifically for testing clients, you can use the start option for interactive work. You can also start the emulator from the SQL Connect VS Code extension.

firebase emulators:start

Connect to a custom PostgreSQL instance

By default, the SQL Connect emulator starts a built-in PGlite instance that runs PostgreSQL 17. To connect the emulator to an existing local PostgreSQL server instead, set the FIREBASE_DATACONNECT_POSTGRESQL_STRING environment variable in your shell or CI/CD environment. Pass a PostgreSQL connection URI when you start the emulator:

export FIREBASE_DATACONNECT_POSTGRESQL_STRING="postgres://USERNAME:PASSWORD@HOST:PORT?sslmode=disable"
firebase emulators:start --only dataconnect

Replace the placeholders with the values for your PostgreSQL server:

  • USERNAME and PASSWORD: The PostgreSQL role credentials for the connection.
  • HOST and PORT: The hostname and port where your PostgreSQL server listens, such as localhost:5432.

The emulator connects to the database set in the database field under schema.datasource.postgresql in your dataconnect.yaml file, overriding any database name in the URI. If that field is omitted, the emulator uses the postgres database.

You can use either the postgres:// or postgresql:// scheme, and you can also set the FIREBASE_DATACONNECT_POSTGRESQL_STRING environment variable when running firebase emulators:exec in CI/CD pipelines. Because the emulator binary requires SSL over TCP by default, include ?sslmode=disable if your local PostgreSQL server doesn't have SSL enabled.

Configuration precedence and PGlite tradeoffs

When the FIREBASE_DATACONNECT_POSTGRESQL_STRING environment variable is set to a non-empty value, it takes precedence over the built-in PGlite server. The Firebase CLI skips starting PGlite and ignores the emulators.dataconnect.postgresPort and emulators.dataconnect.dataDir keys in your firebase.json file.

The following table compares the built-in PGlite instance with a custom PostgreSQL server:

Behavior Built-in PGlite Custom PostgreSQL
PostgreSQL version Runs PostgreSQL 17 locally through PGlite. Runs the version on your server. Use a supported PostgreSQL version.
Extensions Bundles the uuid-ossp and pgvector extensions automatically. Requires you to install needed extensions on your server and grant permission to run CREATE EXTENSION, or pre-install them in the target database.
Persistence and snapshots Persists data in the emulators.dataconnect.dataDir directory if configured, and supports the --import flag, the --export-on-exit flag, and the firebase emulators:export command. Managed by your PostgreSQL server. For SQL Connect, the dataDir key, the --import flag, the --export-on-exit flag, and the firebase emulators:export command have no effect; other emulators still import and export as usual. Stopping the emulator doesn't stop your PostgreSQL server.
Schema migration and data reset Automatically migrates the schema on startup and reload. Reset data with teardown mutations or by removing the dataDir directory. Automatically migrates the target database on startup and reload using the schemaValidation setting from your dataconnect.yaml file. Reset data directly in PostgreSQL or with teardown mutations.

Prerequisites and caveats

Before you connect the emulator to a custom PostgreSQL server, check the following requirements and failure behaviors:

  • No fallback to PGlite: When the FIREBASE_DATACONNECT_POSTGRESQL_STRING environment variable is set, the emulator doesn't fall back to PGlite on errors. If the value isn't a valid URI, for example if it uses key=value syntax or unescaped special characters in credentials, startup fails immediately. If the server isn't reachable, credentials are invalid, or SSL fails, the emulator retries three times and then fails startup.
  • Database creation and role privileges: If the database configured in your dataconnect.yaml file doesn't exist yet, the emulator connects to the postgres or template1 database and tries to run CREATE DATABASE. Either create the database beforehand or use a PostgreSQL role with CREATEDB privileges and permission to create schemas, tables, and extensions.
  • Required extensions: During schema migration, the emulator runs CREATE EXTENSION IF NOT EXISTS for extensions that your schema needs. It installs uuid-ossp for uuidV4() expressions and vector from pgvector for the Vector scalar type. If a required extension isn't installed on the server or the role can't create it, schema migration fails and logs an error to the dataconnect-debug.log file. Operations then return schema errors.

Instrument your client code to talk to the emulator

Set up your in-app configuration or test classes to interact with the SQL Connect emulator as follows.

JavaScript
import { initializeApp } from "firebase/app";
import { connectorConfig } from "@name-of-package";
import { connectDataConnectEmulator, getDataConnect } from 'firebase/data-connect';

// TODO: Replace the following with your app's Firebase project configuration
const firebaseConfig = {
  //...
};

const app = initializeApp(firebaseConfig);

const dataConnect = getDataConnect(app, connectorConfig);
connectDataConnectEmulator(dataConnect, "localhost", 9399);

// Make calls from your app
  
Kotlin Android
val connector = MoviesConnector.instance

// Connect to the emulator on "10.0.2.2:9399"
connector.dataConnect.useEmulator()

// (Alternatively) if you're running your emulator on non-default port:
connector.dataConnect.useEmulator(port = 9999)

// Make calls from your app
  
iOS
let connector = DataConnect.dataConnect(DefaultConnectorClient.connectorConfig)

// Connect to the emulator on "127.0.0.1:9399"
connector.useEmulator()

// (alternatively) if you're running your emulator on non-default port:
connector.useEmulator(port: 9999)

// Make calls from your app
  

Use the emulator for testing and continuous integration

Run containerized Local Emulator Suite images

Installation and configuration of the Local Emulator Suite with containers in a typical CI setup is straightforward.

There are a few issues to note:

  • Emulator binaries are installed and cached at ~/.cache/firebase/emulators/. You may want to add this path to your CI cache configuration to avoid repeated downloads.
  • If you don't have a firebase.json file in your repository, you must add a command line argument to the emulators:start or emulators:exec command to specify which emulators should be started. For example, --only dataconnect.

Clear your database between tests

To reset your test environments between runs, Firebase recommends:

  • Writing dedicated mutations to handle the following:
    • In setup, populate a local database instance with starting data.
    • In teardown, delete modified data from post-test database instance.

How the SQL Connect emulator differs from production

The SQL Connect emulator simulates many features of the server-side product. However, there are some exceptions to be aware of:

  • The version and detailed configuration of PGLite may differ from the version of your production Cloud SQL instance.
  • If you're using the emulator to develop with SQL Connect's pgvector and Vertex API integration, calls to the Cloud Vertex API are made directly, rather than through Cloud SQL's Vertex integration. However, calls to the production API are still made, meaning you must use a real Firebase project, cannot use a demo- project, and costs of the Vertex API will be incurred.