AI Prompt: Postgres SQL Style Guide
A guide from Supabase
Apps on Plentiful run on Supabase's open-source database, sign-in, file storage and live updates, so most of this guide applies. The differences: your app reaches its database through its own server — not Supabase's data API (supabase.from(…)) — there are no Edge Functions (your app's server does that work), and scheduled work uses Jobs. Where it says "Supabase dashboard", use your app's Dashboard. Ask the builder when in doubt.
How to use#
Copy the prompt to a file in your repo.
Use the "include file" feature from your AI tool to include the prompt when chatting with your AI assistant. For example, with GitHub Copilot, use #<filename>, in Cursor, use @Files, and in Zed, use /file.
You can also load the prompt directly into your IDE via the following links:
Prompt#
1# Postgres SQL Style Guide23## General45- Use lowercase for SQL reserved words to maintain consistency and readability.6- Employ consistent, descriptive identifiers for tables, columns, and other database objects.7- Use white space and indentation to enhance the readability of your code.8- Store dates in ISO 8601 format (`yyyy-mm-ddThh:mm:ss.sssss`).9- Include comments for complex logic, using '/_ ... _/' for block comments and '--' for line comments.1011## Naming Conventions1213- Avoid SQL reserved words and ensure names are unique and under 63 characters.14- Use snake_case for tables and columns.15- Prefer plurals for table names16- Prefer singular names for columns.1718## Tables1920- Avoid prefixes like 'tbl\_' and ensure no table name matches any of its column names.21- Always add an `id` column of type `identity generated always` unless otherwise specified.22- Create all tables in the `public` schema unless otherwise specified.23- Always add the schema to SQL queries for clarity.24- Always add a comment to describe what the table does. The comment can be up to 1024 characters.2526## Columns2728- Use singular names and avoid generic names like 'id'.29- For references to foreign tables, use the singular of the table name with the `_id` suffix. For example `user_id` to reference the `users` table30- Always use lowercase except in cases involving acronyms or when readability would be enhanced by an exception.3132#### Examples:3334```sql35create table books (36 id bigint generated always as identity primary key,37 title text not null,38 author_id bigint references authors (id)39);40comment on table books is 'A list of all the books in the library.';41```4243## Queries4445- When the query is shorter keep it on just a few lines. As it gets larger start adding newlines for readability46- Add spaces for readability.4748Smaller queries:4950```sql51select *52from employees53where end_date is null;5455update employees56set end_date = '2023-12-31'57where employee_id = 1001;58```5960Larger queries:6162```sql63select64 first_name,65 last_name66from employees67where start_date between '2021-01-01' and '2021-12-31' and status = 'employed';68```6970### Joins and Subqueries7172- Format joins and subqueries for clarity, aligning them with related SQL clauses.73- Prefer full table names when referencing tables. This helps for readability.7475```sql76select77 employees.employee_name,78 departments.department_name79from80 employees81 join departments on employees.department_id = departments.department_id82where employees.start_date > '2022-01-01';83```8485## Aliases8687- Use meaningful aliases that reflect the data or transformation applied, and always include the 'as' keyword for clarity.8889```sql90select count(*) as total_employees91from employees92where end_date is null;93```9495## Complex queries and CTEs9697- If a query is extremely complex, prefer a CTE.98- Make sure the CTE is clear and linear. Prefer readability over performance.99- Add comments to each block.100101```sql102with103 department_employees as (104 -- Get all employees and their departments105 select106 employees.department_id,107 employees.first_name,108 employees.last_name,109 departments.department_name110 from111 employees112 join departments on employees.department_id = departments.department_id113 ),114 employee_counts as (115 -- Count how many employees in each department116 select117 department_name,118 count(*) as num_employees119 from department_employees120 group by department_name121 )122select123 department_name,124 num_employees125from employee_counts126order by department_name;127```