Simplify Your Development Process with Supabase Local
Learn how to set up Supabase locally. Supabase is an open-source, real-time platform that makes it easy to build and deploy cloud-native applications. Its Supabase CLI feature allows developers to run a local instance of Supabase for development and testing purposes.
Before beginning any development that might require significant changes to the UI or database, it is recommended to set up Supabase Local. Supabase Local is an open-source, realtime platform that makes it easy to build and deploy cloud-native applications, and its Supabase CLI feature allows for a local instance of Supabase to be run for development and testing purposes. In this post, we will walk through the steps to set up Supabase Local and get it up and running on your local machine.
Why Supabase?
Supabase is an open-source, realtime, and fully-featured platform that makes it easy to build and deploy cloud-native applications. One of the key features of Supabase is Supabase CLI, which allows developers to run a local instance of Supabase for development and testing purposes. In this post, we will walk through the steps to set up Supabase Local and get it up and running on your local machine.
Keeping this article as a micro blog as the main documentation is more than enough
Setting up Supabase CLI
To set up Supabase CLI, run the following command globally:
Alternatively, for MacOS users, run:
Learn to read the documentation properly
You can do this in the project that you have the frontend setup already done.
Since the documentation doesn’t clearly say how to do this in a full stack project, what I decided to try was create a server folder in my NextJs+TailwindCSS project(from this article)
I was unable to use the CLI cmd supabase
- before debugging next steps I decided to read documentation again to see if I was supposed to install this globally but there is no clear mention for the same.
But found the OS based tab in the CLI installation documentation and tried it for the MacOS
This worked like a charm and I did this at the global level from my terminal. Which makes me think even for the npm we should have tried globally, not trying it now moving forward with the development.
Logging in to Supabase
Before logging in to Supabase, make sure you have signed up on the Supabase website and completed any necessary verification steps. Then, run the following command in the terminal:
Follow the provided link, generate a token, and use it to log in to the terminal. Do not save the token unless absolutely necessary.
Creating a New Project
Now once the Supabase account is linked with your local setup then I deviated from the original documentation to create a new project, and decided to embed it in the frontend project that I already have.
Just created a server directory at the root level, cd server
then ran the following command
This created the following files
Post this ran the following command:
This will download and start the necessary containers to run Supabase locally. This process may take some time, as some of the containers are large.
Once completed, you will see the following output, which can be used to set up Supabase in your NextJS application:
warning
This is anyway on your local machine but make sure to keep the above secret keys as secrets
You can checkout the Studio URL link, yes there are limitations on local studio which you can find on the official website:
learning
Based on what has transpired so far, no need for the server folder to be inside your project, you can create is separately and maintain things. It is a good idea to to create separate folders for different project which might keep the logical separation of the DB with in local setup (I am not actually sure about this, need to validate this with some documentation or other tutor).
I decided to add this folder to gitignore.
Local setup additional steps performed
As mentioned earlier this is not a much for people who are setting things up on LOCAL first. Since I had a project created on the Supabase Dashabord (PROD) - I had to do the following steps to reproduce the project locally.
- DB setup
- migrated the table schema from PROD to local using the DDL scripts which I was able to retrieve using DBeaver tool and connecting to the PROD DB
- Policy definitions
- Apply the policies from the PROD to LOCAL
- RPC calls setup
- If any add those too
- Authentication enablement
- If anything defined then add those too
- Storage buckets
- Create the same storage buckets
Ideal way
For setting up local and making sure you have promote systems to prod you can follow the instructions on the main documentation
Disclaimer
I was unable to set things up locally due to time limit that I had given to myself. I faced problems in the migration. As per documentation we are supposed to use another docker image to migrate things by we would ideally need to edit the docker compose file to access the docker service host and port and not just rely on the port forwarding to local( psycopg2 threw error )
Could have solved things by following this doc but as mentioned wanted to finish things early that was the whole point of using the supabase as BaaS in the first place.
But apart from the above hiccup I was able to set things up locally by manually migrating the DB schema and setting up things.
Deploying to Production
Before deploying to production, it is important to make the necessary changes to your project, including:
- DB migrations
- RPC calls migrations
- Edge functions
- Other policy updates
- Authentication enablement
- Storage buckets
Conclusion
In summary, Supabase Local is a useful tool for developing and testing cloud-native applications. By following the steps outlined in this post, you can easily set up Supabase Local on your local machine and use it in your NextJS application. Before deploying to production, remember to make the necessary changes to your project to ensure a smooth deployment.
Edit: Jan 06th 2023:
Read about Management API, this will help manage things programatically via some scripts to setup things. I hope supabase or someone comes up with the following flow:
- Developer once he starts local setup, his key actions for supabase are captured like:
- DB updates
- policies and so on
- Then spit out a ansible script kind of thing which can be used for quick PROD deployment
I think the supabase-cli migrations does the same thing but need to check if everything is possible.