Backup and Restore using the CLI
Learn how to backup and restore projects using the Supabase CLI
Backup database using the CLI
Get the new database connection string
Go to the project page and click the "Connect" button at the top of the page for the connection string.
Use the Session pooler connection string by default. If your ISP supports IPv6, use the direct connection string.
Session pooler connection string:
1postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@aws-0-us-east-1.pooler.supabase.com:5432/postgres
Direct connection string:
1postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.com:5432/postgres
Get the database password
Reset the password in the Database Settings.
Replace [YOUR-PASSWORD]
in the connection string with the database password.
Backup database
Run these commands after replacing [CONNECTION_STRING]
with your connection string from the previous steps:
1supabase db dump --db-url [CONNECTION_STRING] -f roles.sql --role-only
1supabase db dump --db-url [CONNECTION_STRING] -f schema.sql
1supabase db dump --db-url [CONNECTION_STRING] -f data.sql --use-copy --data-only
Before you begin
Restore backup using CLI
Configure newly created project
In the new project:
- If Webhooks were used in the old database, enable Database Webhooks.
- If any non-default extensions were used in the old database, enable the Extensions.
- If Replication for Realtime was used in the old database, enable Publication on the tables necessary
Get the new database connection string
Go to the project page and click the "Connect" button at the top of the page for the connection string.
Use the Session pooler connection string by default. If your ISP supports IPv6, use the direct connection string.
Session pooler connection string:
1postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@aws-0-us-east-1.pooler.supabase.com:5432/postgres
Direct connection string:
1postgresql://postgres.[PROJECT-REF]:[YOUR-PASSWORD]@db.[PROJECT-REF].supabase.com:5432/postgres
Get the database password
Reset the password in the Database Settings.
Replace [YOUR-PASSWORD]
in the connection string with the database password.
Restore your Project with the CLI
Run these commands after replacing [CONNECTION_STRING]
with your connection string from the previous steps:
12345678psql \ --single-transaction \ --variable ON_ERROR_STOP=1 \ --file roles.sql \ --file schema.sql \ --command 'SET session_replication_role = replica' \ --file data.sql \ --dbname [CONNECTION_STRING]
Important project restoration notes
Troubleshooting notes
- Setting the
session_replication_role
toreplica
disables all triggers so that columns are not double encrypted. - If you have created any custom roles with
login
attribute, you have to manually set their passwords in the new project. - If you run into any permission errors related to
supabase_admin
during restore, edit theschema.sql
file and comment out any lines containingALTER ... OWNER TO "supabase_admin"
.
Preserving migration history
If you were using Supabase CLI for managing migrations on your old database and would like to preserve the migration history in your newly restored project, you need to insert the migration records separately using the following commands.
12345678supabase db dump --db-url "$OLD_DB_URL" -f history_schema.sql --schema supabase_migrationssupabase db dump --db-url "$OLD_DB_URL" -f history_data.sql --use-copy --data-only --schema supabase_migrationspsql \ --single-transaction \ --variable ON_ERROR_STOP=1 \ --file history_schema.sql \ --file history_data.sql \ --dbname "$NEW_DB_URL"
Schema changes to auth
and storage
If you have modified the auth
and storage
schemas in your old project, such as adding triggers or Row Level Security(RLS) policies, you have to restore them separately. The Supabase CLI can help you diff the changes to these schemas using the following commands.
12supabase link --project-ref "$OLD_PROJECT_REF"supabase db diff --linked --schema auth,storage > changes.sql
Migrate storage objects
The new project has the old project's Storage buckets, but the Storage objects need to be migrated manually. Use this script to move storage objects from one project to another.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152// npm install @supabase/supabase-js@1const { } = ('@supabase/supabase-js')const = 'https://xxx.supabase.co'const = 'old-project-service-key-xxx'const = 'https://yyy.supabase.co'const = 'new-project-service-key-yyy';(async () => { const = (, , { : { : 'storage', }, }) const = (, ) const = (, ) // make sure you update max_rows in postgrest settings if you have a lot of objects // or paginate here const { : , } = await .('objects').() if () { .('error getting objects from old bucket') throw } for (const of ) { .(`moving ${.}`) try { const { , : } = await . .(.) .(.) if () { throw } const { , : } = await . .(.) .(., , { : true, : .., : .., }) if () { throw } } catch () { .('error moving ', ) .() } }})()