Working with PostgreSQL on Supabase: Features and Setup
Introduction to PostgreSQL with Supabase
Supabase is an open-source backend-as-a-service that offers a fully managed PostgreSQL database with additional functionalities like real-time updates, authentication, and serverless functions. Supabase provides developers with a quick and easy way to create powerful, scalable applications by combining PostgreSQL’s robust relational database capabilities with modern features.
Description
PostgreSQL serves as the core database engine in Supabase, allowing users to store structured data, manage relational tables, and perform complex queries. With Supabase, developers can interact with a PostgreSQL database through a simple API, making it an ideal choice for building web and mobile applications that require a relational database. Supabase also includes features like authentication, storage, and real-time data syncing.
Key Supabase PostgreSQL Features
- 1. Real-Time Data Updates: Supabase leverages PostgreSQL’s LISTEN and NOTIFY functionalities to offer real-time data updates. This is especially useful for applications that require immediate data synchronization across clients.
- Row-Level Security (RLS): Supabase enables developers to set up granular access controls using PostgreSQL’s row-level security, allowing for flexible and secure access permissions.
- Automatic API Generation: Supabase generates a RESTful API for your PostgreSQL tables automatically, allowing you to interact with your database through HTTP requests. This API is useful for building client-side applications without needing a backend server.
- Auth and Storage: Supabase provides built-in user authentication and storage capabilities, which integrate seamlessly with PostgreSQL to streamline application development.
Example Setup and Usage with Supabase and PostgreSQL
Here's a quick example of setting up and using Supabase with PostgreSQL:
1. Create a Supabase Project:
- Sign up on Supabase and create a new project.
- Once created, Supabase provides a PostgreSQL database URL and API keys for secure access.
2. Connecting to Supabase PostgreSQL Database:
Code:
-- Connect to your Supabase PostgreSQL database
psql -h your_supabase_host -U your_supabase_user -d your_supabase_database -p your_supabase_port
Explanation:
- Replace your_supabase_host, your_supabase_user, your_supabase_database, and your_supabase_port with the actual credentials provided by Supabase.
- This command connects directly to the PostgreSQL database hosted by Supabase.
3. Creating and Using Tables:
Code:
-- Example of creating a table in Supabase PostgreSQL
CREATE TABLE employees (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
position VARCHAR(100),
created_at TIMESTAMPTZ DEFAULT NOW()
);
Explanation:
- This SQL command creates an employees table within your Supabase PostgreSQL database, which can then be accessed and manipulated through the Supabase auto-generated API.
4. Interacting with Real-Time Features:
Use Supabase’s JavaScript client to receive real-time updates for changes to the employees table.
Code:
import { createClient } from '@supabase/supabase-js';
const supabaseUrl = 'https://your-supabase-url.supabase.co';
const supabaseKey = 'your-supabase-api-key';
const supabase = createClient(supabaseUrl, supabaseKey);
// Subscribe to real-time updates
supabase
.from('employees')
.on('INSERT', (payload) => {
console.log('New employee added:', payload.new);
})
.subscribe();
Explanation:
- This JavaScript code connects to Supabase using your unique API credentials.
- The on('INSERT') method listens for new entries added to the employees table, providing real-time updates when data changes.
Notes:
- Row-Level Security (RLS): For additional security, you can enable RLS on your PostgreSQL tables in Supabase and define policies to control who can access or modify data.
- Scalable Backend: With Supabase, you get the scalability and power of PostgreSQL along with modern backend features, making it an excellent choice for applications that need both relational data and real-time features.
All PostgreSQL Questions, Answers, and Code Snippets Collection.
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://www.w3resource.com/PostgreSQL/snippets/postgresql-supabase.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics