Mock Postgres Table Schema Generator
Generates example PostgreSQL CREATE TABLE statements for testing
Generated 67 words · 558 characters
See 3 examples
- Example 1
CREATE TABLE audit_logs ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, actor_id BIGINT REFERENCES users(id) ON DELETE SET NULL, action VARCHAR(60) NOT NULL, entity_type VARCHAR(60) NOT NULL, entity_id BIGINT NOT NULL, request_id UUID, ip_address INET, changes JSONB, occurred_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX idx_audit_logs_actor_id ON audit_logs (actor_id); CREATE INDEX idx_audit_logs_entity_type_entity_id ON audit_logs (entity_type, entity_id); COMMENT ON TABLE audit_logs IS 'Append-only record of changes';
- Example 2
CREATE TABLE messages ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, sender_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, recipient_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, body TEXT NOT NULL, read_at TIMESTAMPTZ, attachment_url TEXT, deleted_by_sender BOOLEAN NOT NULL DEFAULT false, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now() ); CREATE INDEX idx_messages_sender_id ON messages (sender_id); CREATE INDEX idx_messages_recipient_id ON messages (recipient_id);
- Example 3
CREATE TABLE feature_flags ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, key VARCHAR(80) NOT NULL, environment TEXT NOT NULL CHECK (environment IN ('dev', 'staging', 'production')), is_enabled BOOLEAN NOT NULL DEFAULT false, description TEXT, rollout_pct SMALLINT NOT NULL DEFAULT 0 CHECK (rollout_pct BETWEEN 0 AND 100), created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), UNIQUE (key, environment) ); COMMENT ON TABLE feature_flags IS 'Feature toggles per environment';
About this generator
Reading a CREATE TABLE statement is often the fastest way to understand a data model, but writing a realistic one from scratch — with the right column types, constraints, defaults, and foreign keys — takes longer than it should when you just need an example. This tool generates a sample PostgreSQL CREATE TABLE statement for one of 52 realistic tables across e-commerce, billing, publishing, project management, scheduling, logistics, healthcare, education, and more. There are no inputs. Each table has its own domain-appropriate columns: always-present core columns plus a random selection of optional ones, so the same table comes out differently on different runs. Orders reference customers and carry a CHECK-constrained status and a NUMERIC total; sessions store a hashed token and an expiry; time_entries reference tasks and users; sensor_readings use DOUBLE PRECISION values. The primary key is usually a BIGINT identity column, sometimes a UUID with gen_random_uuid() or a BIGSERIAL. Most tables get created_at and updated_at timestamps, and the output may add one or two CREATE INDEX statements on foreign keys or common filter columns and a COMMENT ON TABLE. Copy the output into a psql session, a migration file, or documentation. Adapt the table and column names to your own domain. The NUMERIC(10,2) type is appropriate for monetary values; avoid FLOAT for money because of floating-point precision errors.
How to use
- Click Generate to produce a table schema.
- Copy the SQL into a migration or console.
- Adapt the columns to your data model.
- Run it after tailoring to your needs.
Use Cases
- •Learning SQL table syntax
- •Seeding a test database schema
- •Documenting a data model
- •Demoing PostgreSQL DDL
- •Testing a migration tool
Tips
- →SERIAL makes an auto-increment id.
- →Use REFERENCES for foreign keys.
- →NUMERIC suits money values.
- →Adapt the schema to your data.
FAQ
what tables can the generator produce
It picks one of 52 tables at random — for example users, orders, order_items, payments, invoices, subscriptions, api_keys, posts, comments, tasks, bookings, shipments, inventory_levels, appointments, loans, enrollments, audit_logs, and sensor_readings. Each has domain-specific columns, foreign keys to related tables, CHECK constraints where they make sense, and an identity, UUID, or BIGSERIAL primary key.
what is a serial primary key in postgres
SERIAL (and BIGSERIAL) is a PostgreSQL shorthand that creates an integer column backed by a sequence, auto-incrementing for each new row. For new schemas, GENERATED ALWAYS AS IDENTITY is the standard SQL equivalent and what this generator uses most often; BIGSERIAL still appears occasionally because it remains widely used and understood.
why use numeric(10,2) instead of float for monetary values
FLOAT stores values as binary floating-point, which cannot represent decimal fractions like 0.10 exactly, causing rounding errors in financial calculations. NUMERIC (also called DECIMAL) stores values at exact precision, making it the correct type for monetary amounts. NUMERIC(10,2) means up to 10 significant digits with exactly 2 decimal places.
can I run the generated statement directly against postgres
The syntax is valid PostgreSQL DDL, but the tables reference each other — for example, orders references users(id) — so if you run an orders CREATE TABLE without the users table existing first, it will fail. Run the statements in dependency order, or adapt the schema to your own data model.
You might also like
Popular tools from other categories that share themes with this one.