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.