w3resource

Getting Started with PostgreSQL and Supabase Integration


PostgreSQL with Supabase: A Comprehensive Overview

Supabase is a backend-as-a-service platform built on PostgreSQL. It provides developers with tools to build and deploy applications quickly while leveraging PostgreSQL's robust relational database features. Here, we explore how Supabase integrates with PostgreSQL, its features, syntax, examples, and practical use cases.

PostgreSQL Overview

PostgreSQL is a powerful, open-source relational database system that supports complex queries, ACID compliance, and extensibility.

Key Features:

  • Advanced indexing, JSON support, and stored procedures.
  • Extensive support for SQL standards.
  • High-performance handling of large-scale data.

What is Supabase?

Description: Supabase is an open-source Firebase alternative that provides a real-time database, authentication, and APIs. It uses PostgreSQL as its core database engine, enabling developers to access PostgreSQL’s advanced capabilities in a serverless environment.

Key Features:

  • Fully managed PostgreSQL database.
  • Real-time capabilities for instant data updates.
  • Built-in user authentication and authorization.
  • RESTful APIs generated automatically for database tables.
  • Easy integration with client-side applications.

Supabase and PostgreSQL Integration

Supabase enhances PostgreSQL by adding developer-friendly features such as auto-generated APIs, real-time subscriptions, and authentication. Developers can interact with the database using the Supabase client libraries or directly through SQL.

Example: Setting up a table using Supabase's SQL editor:

Code:

-- Creating a "Users" table in Supabase (PostgreSQL backend)
CREATE TABLE Users (
    id SERIAL PRIMARY KEY,          -- Auto-incremented primary key
    username VARCHAR(100) NOT NULL, -- Username field
    email VARCHAR(255) UNIQUE,      -- Unique email field
    created_at TIMESTAMP DEFAULT NOW() -- Default timestamp for creation
);

Explanation:

  • SERIAL PRIMARY KEY: Creates an auto-incrementing ID for each user.
  • VARCHAR: Defines character fields for username and email.
  • DEFAULT NOW(): Sets the current timestamp as the default value for created_at.

Working with Supabase Client Libraries

Supabase provides SDKs to interact with the database programmatically.

Example (Using Supabase JavaScript Client):

Code:

// Import the Supabase client
import { createClient } from '@supabase/supabase-js';

// Initialize Supabase with project URL and public API key
const supabase = createClient('https://your-project.supabase.co', 'your-public-anon-key');

// Insert a new user into the "Users" table
const insertUser = async () => {
    const { data, error } = await supabase
        .from('Users')  // Target the "Users" table
        .insert([
            { username: 'janaisabel', email: '[email protected]' } // Inserted data
        ]);

    if (error) console.error('Error:', error);
    else console.log('User added:', data);
};

insertUser();

Explanation

  • createClient: Initializes the Supabase client with project credentials.
  • .from('Users').insert: Specifies the table and data to insert.
  • data, error: Handles the response and potential errors.

Key Advantages of Supabase for PostgreSQL

    1. Real-Time Data: Enables real-time applications without manual polling.

    2. Auto-Generated APIs: Simplifies backend development with instant API endpoints.

    3. Authentication: Provides built-in user management with OAuth and JWT.

    4. Cross-Platform Compatibility: Works seamlessly with modern frameworks (React, Vue, etc.).

PostgreSQL with Supabase: Practical use Cases

  • Real-Time Chat Applications: Utilize Supabase's real-time subscriptions.
  • E-Commerce Platforms: Build robust and scalable systems with advanced SQL features.
  • User Authentication Systems: Integrate secure, built-in authentication.

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/PostgreSQL/snippets/postgresql-and-supabase.php