How to transfer your Back-End from Supabase Cloud to Self-Hosted?
Development platforms like Firebase got more and more popular. Through providing serverless hosting, scalable databases, and other features like authentication, make developing apps easier. While Firebase has a huge position in the market, alternatives are on the rise. One of them: Supabase.
The platform claims to be a Firebase alternative. And actually, it is a Headless CMS. While it doesn’t offer as many features as Firebase, it already offers some important ones: Storage, Authentification, and a Database. And it is not NoSQL, it will give you a Postgres database. an API for interacting with that database that automatically evolves (and is self-documenting!) as your database changes; a user authentication system that works with popular login providers (Facebook, Twitter, Google, Apple, etc.); a storage system for processing things such as image and video uploads; and a user interface to control and manage them all. Building almost any modern app or service takes a ton of work and eliminates it in a few clicks.
What can you do with supabase?
- Can listen to database changes,
- It can query tables like GraphQL, provide pagination and filter complex nested relationships,
- Intervention to the database via the UI Dashboard,
- Editing users and editing their roles
- Ability to process complex queries thanks to PostgreSQL
- Ability to hold all kinds of media with storage
There are several ways to use Supabase:
- Supabase Cloud: you don’t need to deploy anything. Supabase will manage and scale your infrastructure.(It’s free for hobby & small projects)
- Docker: deploy to your own infrastructure.
- Kubernetes: are coming soon.
Pros and cons of self-hosting
There are many reasons why you might want to self-host Supabase:
- More database space — using your own VPS you can have a much larger database than is (currently) available through the platform managed by Supabase.
- Full infrastructure control — you’re in control of everything, so if you need to make a change you have the option
There are also some reasons you might want to let Supabase host your database instead of self-hosting:
- Already installed — No significant technical knowledge is required to set up a Supabase instance on their platform
- Costs — They offer a free plan that may be sufficient for your needs
But bad news: The self-hosted version of Supabase does not include a UI yet.
If you think you can handle this, let’s get started!
1. Before you begin
The first thing you should do is setup somewhere to host. I have used AWS EC2 instance (Ubuntu 20.04). And all information will be given based on this.
You need the following installed in your system:
- Docker and docker compose(v2)
Note: Docker compose version comes 1.25.0 on Ubuntu 20.04 LTS instances. But if you want to compose using docker-compose.yaml, you need to upgrade the docker-compose version to v2. (docker-compose.yaml rules written for v2)
2. Getting Started
I have used a docker image that was given from supabase. After simple setup, you would need to setup storage as AWS S3 and database as AWS RDS. Because:
- the database is in the same machine as the servers
- the storage uses the filesystem backend instead of S3
First, let’s have a look at the current architecture of Supabase.
- PostgreSQL is an object-relational database system with over 30 years of active development that has earned it a strong reputation for reliability, feature robustness, and performance.
- Realtime is an Elixir server that allows you to listen to PostgreSQL inserts, updates, and deletes using websockets. Supabase listens to Postgres’ built-in replication functionality, converts the replication byte stream into JSON, then broadcasts the JSON over websockets.
- PostgREST is a web server that turns your PostgreSQL database directly into a RESTful API
- Storage provides a RESTful interface for managing Files stored in S3, using Postgres to manage permissions.
- postgres-meta is a RESTful API for managing your Postgres, allowing you to fetch tables, add roles, and run queries etc.
- GoTrue is an SWT based API for managing users and issuing SWT tokens.
- Kong is a cloud-native API gateway.
Get the code
Checkout the docker directory in the Supabase repo:
git clone https://github.com/supabase/supabasecd supabase/docker
Copy .env.example to .env and populate the values.
cp .env.example .env
In particular, these are required. If you pass this values as empty, containers won’t work correctly:
POSTGRES_PASSWORD: the password for the postgres roleJWT_SECRET: used by PostgREST and GoTrue, among othersSITE_URL: the base URL of your siteSMTP_*: mail server credentials
.env file example usage:
# Secrets- POSTGRES_PASSWORD=blabla- JWT_SECRET=blabla# Auth## General- SITE_URL=http://localhost:3000- ADDITIONAL_REDIRECT_URLS=- JWT_EXPIRY=3600- DISABLE_SIGNUP=false## Email auth- ENABLE_EMAIL_SIGNUP=true- ENABLE_EMAIL_AUTOCONFIRM=false- SMTP_ADMIN_EMAIL= firstname.lastname@example.org- SMTP_HOST= smtp.blabla.com- SMTP_PORT=587- SMTP_USER= email@example.com- SMTP_PASS=- SMTP_SENDER_NAME=bla
Email verification url was coming in default like
But it must be
You need to change this:
GOTRUE_MAILER_URLPATHS_INVITE: /auth/v1/verifyGOTRUE_MAILER_URLPATHS_CONFIRMATION: /auth/v1/verifyGOTRUE_MAILER_URLPATHS_RECOVERY: /auth/v1/verifyGOTRUE_MAILER_URLPATHS_EMAIL_CHANGE: /auth/v1/verify
Generate API Keys
Use your JWT_SECRET to generate anon and service API keys using the JWT generator (https://jwt.io/). Replace the values in these files:
- ANON_KEY — replace with an anon key
- SERVICE_KEY — replace with an service key
- anon — replace with an anon key
- service_role — replace with an service key
3. Compose & Start
If you feel ready, now you can compose your containers.
docker compose up
Testing with example apps
If you need to test connections and containers, you should select your port as 8000 (API Port). Because, KONG takes requests.
4.Database Configurations (Optional)
If you have a working system on supabase.io, you can use code that shows example data export from supabase.io
pg_dump -h db.blabla.supabase.co -U postgres — clean — schema-only > supabase_schema.sql
pg_dump -h db.blabla.supabase.co -U postgres — data-only -n storage > supabase_data.sql
And here we go: It’s done!