If you’re using Supabase for your project, you might have noticed that Supabase pauses your database after a period of inactivity. This is a common feature in free-tier plans to save resources, but it can be frustrating if you want your database to remain active at all times. Fortunately, you can prevent this by setting up a simple automated “ping” to your Supabase database using GitHub Actions.
In this blog post, I’ll walk you through how to use a GitHub Actions workflow to keep your Supabase database active and prevent it from being paused. I’ll also explain the code and how it works.
Why Does Supabase Pause Your Database?
Supabase, like many other database-as-a-service providers, pauses your database after a period of inactivity to conserve resources. This is especially true for free-tier projects. While this is great for saving costs, it can be inconvenient if you want your database to remain active for testing, development, or other purposes.
To prevent this, you can periodically “ping” your database by running a simple query. This keeps the database active and prevents it from being paused.
Automating Database Pings with GitHub Actions
GitHub Actions is a powerful tool for automating workflows. You can use it to schedule tasks, such as pinging your Supabase database, at regular intervals. Below, I’ll explain how to set up a GitHub Actions workflow to ping your Supabase database every Monday and Thursday at 9:00 AM UTC.
The GitHub Actions Workflow
Here’s the GitHub Actions workflow code that you can use to ping your Supabase database:
name: Ping Supabase to Prevent Pausing
on:
schedule:
- cron: '0 9 * * 1,4' # Runs at 9:00 AM UTC every Monday and Thursday
workflow_dispatch: # Allows manual triggering from the GitHub UI
jobs:
ping:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the repository
- name: Checkout repository
uses: actions/checkout@v3
# Step 2: Set up Node.js
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '18' # Use Node.js 18
# Step 3: Install Supabase Client
- name: Install Supabase Client
run: npm install @supabase/supabase-js --force
# Step 4: Ping Supabase
- name: Ping Supabase
env:
SUPABASE_URL: $ secrets.NEXT_PUBLIC_SUPABASE_URL # Supabase project URL
SUPABASE_KEY: $ secrets.NEXT_SERVICE_ROLE_KEY # Supabase service role key
run: |
node -e "
(async () =>
try
// Debugging: Log environment variables (optional)
console.log('Supabase URL:', process.env.SUPABASE_URL);
console.log('Supabase Key:', process.env.SUPABASE_KEY);
// Import Supabase client
const createClient = require('@supabase/supabase-js');
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_KEY);
// Ping Supabase by querying a table (e.g., 'songs')
const data, error = await supabase.from('your_table').select('*').limit(10);
// Handle errors
if (error) throw error;
// Log success
console.log('Ping successful:', data);
catch (err)
// Log and exit with error
console.error('Error pinging Supabase:', err.message);
process.exit(1);
)();
"
How It Works
-
Triggering the Workflow:
- The workflow is triggered on a schedule using the
cron
syntax (0 9 * * 1,4
). This means it will run at 9:00 AM UTC every Monday and Thursday. - You can also manually trigger the workflow using the
workflow_dispatch
event.
- The workflow is triggered on a schedule using the
-
Setting Up the Environment:
- The workflow checks out your repository and sets up Node.js (version 18 in this case).
- It then installs the Supabase client (
@supabase/supabase-js
) using npm.
-
Pinging Supabase:
- The workflow uses the Supabase client to connect to your database using the
SUPABASE_URL
andSUPABASE_KEY
environment variables. - It runs a simple query (e.g., selecting 10 rows from a table) to keep the database active.
- If the ping is successful, it logs the data. If there’s an error, it logs the error and exits with a non-zero status code.
- The workflow uses the Supabase client to connect to your database using the
Setting Up Environment Variables
To make this workflow work, you need to set up the following environment variables in your GitHub repository:
-
NEXT_PUBLIC_SUPABASE_URL
: Your Supabase project URL. -
NEXT_SERVICE_ROLE_KEY
: Your Supabase service role key.
You can add these secrets in your GitHub repository under Settings > Secrets and variables > Actions.
Customizing the Workflow
-
Change the Schedule: If you want to ping your database more or less frequently, you can modify the
cron
schedule. For example,0 9 * * *
would run the workflow every day at 9:00 AM UTC. -
Change the Table: Replace
'your_table'
with the name of a table in your Supabase database. -
Add More Debugging: If you want to debug the workflow further, you can add more
console.log
statements.
Benefits of This Approach
- Keeps Your Database Active: By pinging your database regularly, you ensure that it remains active and doesn’t get paused.
- Automated and Hands-Off: Once set up, the workflow runs automatically, so you don’t have to worry about manually pinging your database.
- Cost-Effective: This solution is free if you’re using GitHub Actions within the free tier limits.
Conclusion
Preventing your Supabase database from being paused is easy with GitHub Actions. By setting up a simple workflow to ping your database at regular intervals, you can ensure that your database remains active and ready for use. This approach is especially useful for free-tier projects or development environments where you need consistent database availability.
Give it a try, and let me know if you have any questions or need further assistance!
Follow me on : Github Linkedin Threads Youtube Channel