You are currently viewing the new Beta Editor Docs.
Switch to the Classic Editor Docs
You are currently viewing the Classic Editor Docs.
Switch to the Beta Editor Docs

Using Multiple Databases

Each Anvil app can have multiple databases. You might want different deployment environments to use different databases – for example, so you can test your app without affecting production data. You can choose which database each environment uses. The tables in this database will be available via anvil.tables.app_tables in this environment.

Adding an additional database

To add an additional database to your Anvil app, open “Data” in the Sidebar Menu and click “+ Add New Database”.

This will create a new database in your app with copies of the data tables from the default database. The data tables in the new database won’t contain any data.

Switching between databases in the editor

To change which database your app uses when it runs in the Editor, click on the three dots next to the + Add Table button of your desired database.

'Use for development' option highlighted in the database panel.

Then select Use for development. The colour of the star, next to the database name, will change to denote which database the Editor is currently using.

Database in use highlighted by a differently coloured star.

The database the Editor is using

Assigning databases to environments

To use your new database in an environment, click the Publish button in the top right of the editor:

This will open the Environments dialog:

Click “Advanced settings” and you will see a database section where you can select which database the environment should use.

Migrating databases

A database schema contains information on the structure of a database i.e. what tables and columns a database should have. Each Anvil app has a database schema and when you load an app in the editor, Anvil checks to make sure the database the app is using matches the app’s database schema.

With multiple databases, the databases can become out of sync - for example, you might have added a new table to your development database.

The next time you begin using a database that is out of sync with the app’s schema, you will be prompted to resolve the schema mismatch.

Clicking the Resolve button in the prompt will open the database schema window which will give you two options in large blue buttons. The options are, either the app is correct or the database is correct.

The app is correct

When selecting The app is correct you are carrying out a destructive action. If a table or column has been deleted in the schema, it will delete it from the database along with the data that table or column contained.

Selecting The app is correct will update the database structure to match the app’s database schema. This includes adding and deleting tables and columns.

The database is correct

Selecting the database is correct will update the app’s database schema to match the database structure.