DDLBuilderOpen DDLBuilder
DDLBuilder
Open DDLBuilder
Docs / Database solutions

PostgreSQL CREATE TABLE & Index Generator

DDLBuilder generates native PostgreSQL DDL in your browser: GENERATED BY DEFAULT AS IDENTITY keys, schema-qualified names, standalone COMMENT ON statements, ALTER TABLE ... ADD CONSTRAINT primary keys, CREATE [UNIQUE] INDEX definitions with ASC/DESC ordering, and WITH (fillfactor = ...) storage options.

Verified DDL Example: Payment Orders Table with Indexes

Click the example button on this page to open this PostgreSQL app.payment_orders table in a fresh draft tab:

CREATE TABLE app.payment_orders (
  id BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL,
  order_no VARCHAR(64) NOT NULL,
  user_id BIGINT NOT NULL,
  amount_cents BIGINT NOT NULL DEFAULT 0,
  currency CHAR(3) NOT NULL DEFAULT 'CNY',
  metadata JSONB,
  paid_at TIMESTAMP WITH TIME ZONE,
  created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
) WITH (fillfactor = 90);
COMMENT ON TABLE app.payment_orders IS '支付订单表';
COMMENT ON COLUMN app.payment_orders.id IS '主键ID';
COMMENT ON COLUMN app.payment_orders.order_no IS '外部订单号';
COMMENT ON COLUMN app.payment_orders.user_id IS '用户ID';
COMMENT ON COLUMN app.payment_orders.amount_cents IS '金额(分)';
COMMENT ON COLUMN app.payment_orders.currency IS '币种';
COMMENT ON COLUMN app.payment_orders.metadata IS '扩展元数据';
COMMENT ON COLUMN app.payment_orders.paid_at IS '支付时间';
COMMENT ON COLUMN app.payment_orders.created_at IS '创建时间';

ALTER TABLE app.payment_orders ADD CONSTRAINT pk_payment_orders PRIMARY KEY (id);
CREATE UNIQUE INDEX uk_payment_orders_order_no ON app.payment_orders (order_no ASC);
CREATE INDEX idx_payment_orders_user_created ON app.payment_orders (user_id ASC, created_at DESC);

Key Capabilities

  • Native PostgreSQL syntax: Emits standalone COMMENT ON, named PRIMARY KEY constraints, and CREATE [UNIQUE] INDEX statements rather than generic SQL approximations.
  • Schema namespaces: Setting a schema name carries it consistently through DDL, comments, indexes, and Prisma @@schema(...).
  • Storage options: Configure tablespace and fillfactor in Advanced → Table options.
  • Citus distributed tables: Switch to PostgreSQL (Citus) to configure reference or distributed tables and emit SELECT create_distributed_table(...).
  • Schema diff & compatibility check: Compare two PostgreSQL schemas for migration SQL or assess MySQL-to-PostgreSQL compatibility.

Steps

  1. Open the preset app.payment_orders table in DDLBuilder using the button on this page.
  2. Customize the schema name, column types (JSONB, UUID, TIMESTAMPTZ), and defaults.
  3. Configure unique and composite indexes with sort directions in the Indexes tab.
  4. Set fillfactor or tablespace under Advanced → Table options.
  5. Copy the generated DDL or switch to ORM for Prisma, TypeORM, SQLAlchemy, GORM, or JPA models.

FAQ

How are table and column comments generated for PostgreSQL?

PostgreSQL does not allow inline column comments inside CREATE TABLE. DDLBuilder automatically outputs standard COMMENT ON TABLE and COMMENT ON COLUMN statements after the table definition.

Does it support PostgreSQL IDENTITY columns, JSONB, and TIMESTAMPTZ?

Yes. Enabling auto-increment emits GENERATED BY DEFAULT AS IDENTITY, and column types support JSONB, TIMESTAMPTZ, CHAR, VARCHAR, and other native PostgreSQL types.

Is PostgreSQL Citus sharding supported?

Yes. Switch the dialect to PostgreSQL (Citus) to configure reference or distributed tables and emit create_distributed_table statements.

Last updated:

Need more help?

Start with the FAQ. If that does not solve it, tell us.

Verified DDL Example: Payment Orders Table with IndexesKey CapabilitiesStepsFAQHow are table and column comments generated for PostgreSQL?Does it support PostgreSQL IDENTITY columns, JSONB, and TIMESTAMPTZ?Is PostgreSQL Citus sharding supported?Related Guides