Home / CodeSQL / SQL / Writing a Professional Database Schema Migration in PostgreSQL

Writing a Professional Database Schema Migration in PostgreSQL

Writing a Professional Database Schema Migration in PostgreSQL
  • Category SQL
  • Type Query
  • Platform Cross-platform
  • Language SQL
  • Price Free
  • Copy 7 900
  • Comments 0
Go to Code
Writing a Professional Database Schema Migration in PostgreSQL

Writing a Professional Database Schema Migration in PostgreSQL

Copy this SQL snippet on Clayi Code — one click, no account required. Free SQL snippets with instant copy — a handy reference for developers and DB admins.

What it does and when to use

A ready-made query or statement for reads, writes, joins, migrations, or routine database admin tasks. Good for reporting, data fixes, schema checks, indexing ideas, or everyday DBA work.

How to copy from Clayi Code

Paste into DBeaver, MySQL Workbench, pgAdmin, mysql/psql CLI, or your app's SQL runner.

Safe SQL habits

Back up tables before UPDATE or DELETE; run a SELECT preview when data changes. On large tables, check the execution plan and add indexes if needed. Never run untested DDL or bulk updates on live data — validate in staging first.

SQL reference

Category: SQL. Dialect: SQL. Platform: Cross-platform. Format: Query.

Popularity
0%
  • Votes: 756
  • Comments: 0

Writing a Professional Database Schema Migration in PostgreSQL

-- --- UP MIGRATION (Apply Changes) ---

-- Create modern snippets table with strict foreign key constraints
CREATE TABLE snippets (
    id SERIAL PRIMARY KEY,
    user_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    code_content TEXT NOT NULL,
    slug VARCHAR(100) UNIQUE NOT NULL,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
);

-- Add an index on the slug column for lighting-fast search queries
CREATE INDEX idx_snippets_slug ON snippets(slug);


-- --- DOWN MIGRATION (Rollback Changes) ---
-- DROP TABLE IF EXISTS snippets;
Free snippet — copy & paste!
Copy this snippet for free on Clayi Code. One click — no account required.

Similar Snippets

Populer Snippets

There are no comments yet :(

Writing a Professional Database Schema Migration in PostgreSQL
Tell us what you think about "Writing a Professional Database Schema Migration in PostgreSQL"
Information
Users of Guests are not allowed to comment this publication.