← All schemas
Schema Library

E-commerce Database Schema Diagram

Every online store needs the same core 11 tables. Customers have addresses and carts; carts hold cart items that reference products; products belong to categories and have images; orders snapshot cart items into order items and collect payments; reviews link customers to products. This modeled e-commerce schema covers catalog, cart, checkout, and post-purchase flows for a custom PostgreSQL storefront.

Written by Varun Krishnan · Founder, dbdiagramr

Published · Updated

Source: Original reference design by dbdiagramr

Tables
11
Core table
orders
Pattern
Original reference design

Original reference design for a custom PostgreSQL storefront, not a published platform schema.

Tax, shipping-rate, discount, fulfillment, and refund tables are omitted; nested commerce objects are flattened into ordinary PostgreSQL columns. Original reference design.

E-commerce database schema diagram showing every table, column, and foreign key

Scroll horizontally to see the full diagram. Click it to open at full size.

Open in DBdiagramr to edit →

Embed this diagram

Paste this HTML into a README, docs page, or blog post. The image links back to the full schema page.

<a href="https://www.dbdiagramr.space/schema/ecommerce">
  <img src="https://www.dbdiagramr.space/schemas/ecommerce-er-diagram.svg" alt="E-commerce Database Schema Diagram (PostgreSQL)" width="1528" height="1486" />
</a>
<p><a href="https://www.dbdiagramr.space/schema/ecommerce">E-commerce Database Schema Diagram (PostgreSQL)</a> by dbdiagramr</p>

Tables in the E-commerce schema

ColumnTypeNullableKey
customers
iduuidNoPK
emailvarcharNo-
first_namevarcharYes-
last_namevarcharYes-
phonevarcharYes-
created_attimestamptzNo-
updated_attimestamptzYes-
addresses
iduuidNoPK
customer_iduuidNoFK → customers.id
labelvarcharYes-
line1varcharNo-
line2varcharYes-
cityvarcharNo-
statevarcharYes-
postal_codevarcharNo-
countryvarchar(2)No-
is_defaultboolNo-
categories
iduuidNoPK
namevarcharNo-
slugvarcharNo-
parent_iduuidYesFK → categories.id
descriptiontextYes-
created_attimestamptzNo-
products
iduuidNoPK
titlevarcharNo-
slugvarcharNo-
descriptiontextYes-
category_iduuidYesFK → categories.id
price_centsintegerNo-
currencyvarchar(3)No-
inventory_countintegerNo-
is_activeboolNo-
created_attimestamptzNo-
updated_attimestamptzYes-
product_images
iduuidNoPK
product_iduuidNoFK → products.id
urltextNo-
alt_textvarcharYes-
positionintegerNo-
created_attimestamptzNo-
carts
iduuidNoPK
customer_iduuidYesFK → customers.id
statusvarcharNo-
created_attimestamptzNo-
updated_attimestamptzYes-
expires_attimestamptzYes-
cart_items
iduuidNoPK
cart_iduuidNoFK → carts.id
product_iduuidNoFK → products.id
quantityintegerNo-
unit_price_centsintegerNo-
added_attimestamptzNo-
orders
iduuidNoPK
customer_iduuidNoFK → customers.id
statusvarcharNo-
total_centsintegerNo-
currencyvarchar(3)No-
shipping_address_iduuidYesFK → addresses.id
billing_address_iduuidYesFK → addresses.id
placed_attimestamptzNo-
paid_attimestamptzYes-
shipped_attimestamptzYes-
order_items
iduuidNoPK
order_iduuidNoFK → orders.id
product_iduuidNoFK → products.id
product_titlevarcharNo-
quantityintegerNo-
unit_price_centsintegerNo-
total_centsintegerNo-
created_attimestamptzNo-
payments
iduuidNoPK
order_iduuidNoFK → orders.id
providervarcharNo-
provider_payment_idtextYes-
amount_centsintegerNo-
currencyvarchar(3)No-
statusvarcharNo-
created_attimestamptzNo-
reviews
iduuidNoPK
product_iduuidNoFK → products.id
customer_iduuidNoFK → customers.id
ratingsmallintNo-
titlevarcharYes-
bodytextYes-
created_attimestamptzNo-

Table relationships

15 relationships connect the tables in this schema. Each entry shows the child columns, the parent columns they reference, and the cardinality.

One-to-many

addresses.customer_id → customers.id

One customer can have many addresses (addresses.customer_id -> customers.id, required).

Self-referencing

categories.parent_id → categories.id

One category can have many child categories (categories.parent_id -> categories.id, optional).

One-to-many

products.category_id → categories.id

One category can have many products (products.category_id -> categories.id, optional).

One-to-many

product_images.product_id → products.id

One product can have many product images (product_images.product_id -> products.id, required).

One-to-many

carts.customer_id → customers.id

One customer can have many carts (carts.customer_id -> customers.id, optional).

One-to-many

cart_items.cart_id → carts.id

One cart can have many cart items (cart_items.cart_id -> carts.id, required).

One-to-many

cart_items.product_id → products.id

One product can have many cart items (cart_items.product_id -> products.id, required).

One-to-many

orders.customer_id → customers.id

One customer can have many orders (orders.customer_id -> customers.id, required).

One-to-many

orders.shipping_address_id → addresses.id

One address can have many orders (orders.shipping_address_id -> addresses.id, optional).

One-to-many

orders.billing_address_id → addresses.id

One address can have many orders (orders.billing_address_id -> addresses.id, optional).

One-to-many

order_items.order_id → orders.id

One order can have many order items (order_items.order_id -> orders.id, required).

One-to-many

order_items.product_id → products.id

One product can have many order items (order_items.product_id -> products.id, required).

One-to-many

payments.order_id → orders.id

One order can have many payments (payments.order_id -> orders.id, required).

One-to-many

reviews.product_id → products.id

One product can have many reviews (reviews.product_id -> products.id, required).

One-to-many

reviews.customer_id → customers.id

One customer can have many reviews (reviews.customer_id -> customers.id, required).

Common queries

These PostgreSQL queries run against the schema shown on this page.

Revenue by day

Sum completed order value per day.

select date_trunc('day', placed_at) as day,
       sum(total_cents) / 100.0 as revenue
from orders
where status <> 'cancelled'
group by 1
order by 1 desc;

Active carts

Show open carts, owners, item counts, and value.

select c.id, coalesce(cu.email, 'guest') as customer,
       count(ci.id) as items,
       coalesce(sum(ci.unit_price_cents * ci.quantity), 0) / 100.0 as value
from carts c
left join customers cu on cu.id = c.customer_id
left join cart_items ci on ci.cart_id = c.id
where c.status = 'active'
group by c.id, cu.email
order by value desc;

Best sellers

Rank products by units sold using the order-item title snapshot.

select product_title, sum(quantity) as units,
       sum(total_cents) / 100.0 as revenue
from order_items
group by product_title
order by units desc, product_title;

Orders without a payment

Find orders that have no payment attempt yet.

select o.id, o.status, o.total_cents, o.placed_at
from orders o
left join payments p on p.order_id = o.id
where p.id is null
order by o.placed_at;

How an order moves through the schema

The schema separates browsing from buying. Products and categories describe the catalog; carts and cart items describe an active session; orders and order items preserve what was actually bought.

Browse to cart. A customer browses products, joins categories and product_images, and adds rows to cart_items through a carts row. Cart rows are mutable: quantities change and items are removed.

Cart to order. At checkout, cart items are copied into order_items. The order row records the total, the chosen shipping and billing addresses, and timestamps. The order item keeps product_id, product_title, unit price, and line total from that moment even if the catalog changes later.

Order to payment to review. payments records each charge attempt against the order. After fulfillment, reviews links the customer and product.

select o.id, o.status, o.total_cents, p.status as payment_status,
       oi.quantity, pr.title
from orders o
join order_items oi on oi.order_id = o.id
join products pr on pr.id = oi.product_id
left join payments p on p.order_id = o.id
where o.customer_id = $1
order by o.placed_at desc;

Design decisions worth copying

  • Cart vs order: mutable vs immutable. cart_items can change; order_items never should. Copy, do not move, at checkout.
  • Addresses are separate from customers. One customer can have home, work, and other addresses. Orders reference a specific address row so the address at order time is preserved after a move.
  • Categories are self-referential. categories.parent_id points at categories.id, which builds a tree such as Electronics → Phones → Cases without a separate hierarchy table.
  • Money is integer cents. Avoid floating-point rounding. Store price_cents integer with a currency column.
  • Payments are attempts, not a boolean. A separate payments row per attempt preserves retries and failures instead of overwriting one status field.

Frequently asked questions

Related schema diagrams

Visualize your own database

Paste your PostgreSQL connection string and get an interactive ER diagram of your own schema in under 10 seconds. No signup required.

Try it free →