Connecting a Supabase Database to a Hostinger Node.js Application

Connecting a Node.js application to a Supabase PostgreSQL database on Hostinger

Updated 3 weeks ago

This guide explains how to connect a Supabase project to a Node.js application hosted on Hostinger using the Supabase JavaScript client.

Before we start, ensure:

  • You already have a built Node.js application

  • Your database is hosted on Supabase (third-party provider)

When using a third-party database provider (like Supabase), Hostinger supports Node.js applications to connect to externally hosted databases such as:

  • PostgreSQL

  • MySQL

  • MongoDB


Step 1: Get Supabase Project Credentials

  1. Log in to the Supabase Dashboard

  2. Open your Supabase project

  3. Navigate to Project Settings → API

  4. Locate:

    • Project URL

    • anon public API key

You will use these values as environment variables.


Step 2: Configure Environment Variables

To connect quickly to Supabase, use the following environment variables.

Create or update your .env file:

SUPABASE_URL=https://your-project-id.supabase.co
SUPABASE_ANON_KEY=your-public-anon-key

Environment variable explanation

  • SUPABASE_URL – Your Supabase project URL

  • SUPABASE_ANON_KEY – Your Supabase anonymous (public) API key

⚠️ The anon key is safe to use in server-side Node.js applications, but it should still be stored securely using environment variables and never hardcoded.

Step 3: Ensure Your Website Contains a Supabase Connection File

Your Node.js application should include a dedicated Supabase client file to handle database communication.

This file is commonly located in:

  • config/supabase.js

  • lib/supabaseClient.js

  • supabase.js


Default Supabase Client Example (Node.js)

import { createClient } from "@supabase/supabase-js";

const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY
);

export default supabase;

This client:

  • Authenticates using your Supabase project credentials

  • Allows access to the database, authentication, and storage APIs

  • Requires no direct database connection handling


Step 4: Deploy application with environment variables

  1. Go to hPanel > Deployments

  2. Select Settings and redeploy

  3. Create SupaBase environment variables

  4. Click save and redeploy


Step 5: Create Database Tables in Supabase

  1. Go to Supabase Dashboard → SQL Editor

  2. Enter SQL query based on the information you’ll be storing

  3. Ensure column names match what your application expects


Step 6: Test Database Operations in Production

Example query using the Supabase client:

const { data, error } = await supabase
.from("restaurant_workers")
.insert([
{
first_name: "John",
last_name: "Doe",
email: "john@example.com",
},
]);

if (error) {
console.error(error);
}
  1. Trigger the operation from your live website

  2. Check the Supabase Table Editor

  3. Confirm the record was inserted successfully

Congratulations, you’ve successfully connected your SupaBase external database to your Hostinger Node.js application.