Featured image of post How to Use PostgreSQL with Supabase

How to Use PostgreSQL with Supabase

Learn how to use PostgreSQL with Supabase, covering database creation, SQL queries, and data management in a step-by-step guide with FAQs.

Supabase offers a seamless way to work with PostgreSQL, providing developers with a powerful backend service for building scalable applications. In this guide, you’ll learn how to use PostgreSQL with Supabase—from setting up your database to running SQL queries and managing data through Supabase’s intuitive interface.

Setting Up PostgreSQL with Supabase

1. Creating a Supabase Project

  • Sign Up and Login: Start by signing up for a free account on Supabase. Once logged in, create a new project by selecting “New Project.”
  • Project Configuration: Enter a name for your project, choose your region, and set a strong password for your database. Supabase will automatically configure PostgreSQL as your database engine.

2. Accessing the Database

  • Dashboard Overview: After creating your project, you’ll be directed to the Supabase dashboard, where you can manage your database.
  • Database Connection: Navigate to the “Database” tab to view connection details, such as your database URL, username, and password.

Creating and Managing a PostgreSQL Database

1. Creating a Table

  • Using the SQL Editor:
    • Access the “SQL Editor” from the dashboard.

    • Run the following SQL query to create a table:

      1
      2
      3
      4
      5
      6
      
      CREATE TABLE tasks (
        id serial PRIMARY KEY,
        task_name text NOT NULL,
        is_completed boolean DEFAULT false,
        created_at timestamp DEFAULT now()
      );
      
    • This command creates a tasks table with columns for id, task_name, is_completed, and created_at.

2. Inserting Data into the Table

  • Insert Data Query:
    • Use the following SQL query to add a new task:

      1
      
      INSERT INTO tasks (task_name) VALUES ('Learn PostgreSQL with Supabase');
      
    • Run the query to insert the data into the tasks table.

3. Retrieving Data

  • Select Query:
    • To view the tasks, run:

      1
      
      SELECT * FROM tasks;
      
    • This query retrieves all records from the tasks table.


Managing Data with Supabase’s Interface

1. Using the Table Editor

  • Editing Data:
    • Access the “Table Editor” under the “Database” tab.
    • You can directly add, edit, or delete rows in your tasks table without writing SQL.

2. Using Supabase’s API

  • API Overview:
    • Supabase automatically generates RESTful APIs for your PostgreSQL tables.
    • Check the “API” tab in the dashboard for documentation and examples.
  • Client Libraries:
    • Utilize Supabase’s JavaScript client to interact with your database programmatically.

Advanced PostgreSQL Queries

1. Updating Records

  • Update Query:

    1
    
    UPDATE tasks SET is_completed = true WHERE id = 1;
    
    • This command marks the task with id = 1 as completed.

2. Deleting Records

  • Delete Query:

    1
    
    DELETE FROM tasks WHERE id = 1;
    
    • This removes the task with id = 1 from the tasks table.

3. Joining Tables

  • Using JOINs:
    • As you create more tables, you can link them using SQL joins. For example:

      1
      2
      3
      
      SELECT tasks.task_name, users.username
      FROM tasks
      JOIN users ON tasks.user_id = users.id;
      

Frequently Asked Questions (FAQs)

1. What is Supabase?

  • Supabase is an open-source backend-as-a-service (BaaS) that provides features like databases, authentication, and storage, built on top of PostgreSQL.

2. How do I connect to PostgreSQL in Supabase?

  • Supabase provides connection details in the dashboard under the “Database” tab. Use these credentials in your PostgreSQL client or application.

3. Can I use Supabase for real-time updates?

  • Yes, Supabase supports real-time updates with PostgreSQL through its built-in WebSocket features.

4. Is Supabase secure?

  • Supabase includes built-in security features like row-level security (RLS) and role-based access controls to protect your data.

Conclusion

Learning how to use PostgreSQL with Supabase empowers you to create robust, scalable applications with ease. From database creation to managing data and running SQL queries, Supabase provides all the tools you need in one platform. Dive deeper into Supabase’s features and take full advantage of PostgreSQL’s power within your projects.