docs/platform/infrastructure/import-cloud-sql.md
When deploying applications to your own cloud, Encore Cloud can provision all necessary infrastructure—including database instances. However, if you already have a Cloud SQL instance, you can connect your Encore application directly to this existing database.
Using an existing Cloud SQL instance allows you to:
Follow these steps to import your existing Cloud SQL instance:
Encore GCP Service Account from the cloud dashboardOwner role to the Encore GCP Service AccountGCP Project ID and Cloud SQL Instance NameResolve button to validate the instanceOnce validated, you can create the environment. When you deploy to this environment, Encore Cloud will automatically connect your application to your imported Cloud SQL instance rather than provisioning a new database.
To access an existing database in your Encore application, you need to specify the name of the existing database when you declare the database in your app. For example, if you have an existing database called mydb you can create a reference to it like so:
const db = new SQLDatabase("mydb");
sqldb.NewDatabase("mydb", sqldb.DatabaseConfig{
Migrations: "./migrations",
})
Encore uses a table called schema_migrations in the public namespace to keep track of which migrations have been applied. If you import an existing database without that table, Encore will create it for you and apply your migrations in order. If the table already exists, Encore expects it to contain exactly two columns:
version bigint
dirty boolean
If the table exists but has a different schema, you will not be able to import it with Encore at this time. If the table exists with an existing entry, Encore will apply all higher versions in your migrations directory to the database.
If you have an existing production database that wasn't previously managed by Encore's migration system, you need to set up a bootstrap migration so that new environments (dev, staging, etc.) are created with the same schema, while your existing database is not modified.
Step 1: Dump your existing database schema:
$ pg_dump --schema-only --no-owner --no-privileges your_database > 001_bootstrap.up.sql
Review the output and clean it up as needed (remove any database-specific settings, comments, etc.), then place it in your service's migrations directory.
Step 2: In your existing imported database, manually create the schema_migrations table and mark the bootstrap migration as already applied:
CREATE TABLE IF NOT EXISTS schema_migrations (version bigint NOT NULL, dirty boolean NOT NULL);
INSERT INTO schema_migrations (version, dirty) VALUES (1, false);
This tells Encore that migration 1 (the bootstrap) has already been applied to this database. Encore will skip it and only run migrations with a higher version number.
Step 3: Any future schema changes should be added as new migration files (e.g. 002_add_column.up.sql). These will be applied to all environments, including the imported database.
This approach ensures that new environments get the full schema from scratch via the bootstrap migration, while your production database continues from where it is without any changes being re-applied.
</Callout>