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;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
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.
Scroll horizontally to see the full diagram. Click it to open at full size.
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>| Column | Type | Nullable | Key |
|---|---|---|---|
| customers | |||
| id | uuid | No | PK |
| varchar | No | - | |
| first_name | varchar | Yes | - |
| last_name | varchar | Yes | - |
| phone | varchar | Yes | - |
| created_at | timestamptz | No | - |
| updated_at | timestamptz | Yes | - |
| addresses | |||
| id | uuid | No | PK |
| customer_id | uuid | No | FK → customers.id |
| label | varchar | Yes | - |
| line1 | varchar | No | - |
| line2 | varchar | Yes | - |
| city | varchar | No | - |
| state | varchar | Yes | - |
| postal_code | varchar | No | - |
| country | varchar(2) | No | - |
| is_default | bool | No | - |
| categories | |||
| id | uuid | No | PK |
| name | varchar | No | - |
| slug | varchar | No | - |
| parent_id | uuid | Yes | FK → categories.id |
| description | text | Yes | - |
| created_at | timestamptz | No | - |
| products | |||
| id | uuid | No | PK |
| title | varchar | No | - |
| slug | varchar | No | - |
| description | text | Yes | - |
| category_id | uuid | Yes | FK → categories.id |
| price_cents | integer | No | - |
| currency | varchar(3) | No | - |
| inventory_count | integer | No | - |
| is_active | bool | No | - |
| created_at | timestamptz | No | - |
| updated_at | timestamptz | Yes | - |
| product_images | |||
| id | uuid | No | PK |
| product_id | uuid | No | FK → products.id |
| url | text | No | - |
| alt_text | varchar | Yes | - |
| position | integer | No | - |
| created_at | timestamptz | No | - |
| carts | |||
| id | uuid | No | PK |
| customer_id | uuid | Yes | FK → customers.id |
| status | varchar | No | - |
| created_at | timestamptz | No | - |
| updated_at | timestamptz | Yes | - |
| expires_at | timestamptz | Yes | - |
| cart_items | |||
| id | uuid | No | PK |
| cart_id | uuid | No | FK → carts.id |
| product_id | uuid | No | FK → products.id |
| quantity | integer | No | - |
| unit_price_cents | integer | No | - |
| added_at | timestamptz | No | - |
| orders | |||
| id | uuid | No | PK |
| customer_id | uuid | No | FK → customers.id |
| status | varchar | No | - |
| total_cents | integer | No | - |
| currency | varchar(3) | No | - |
| shipping_address_id | uuid | Yes | FK → addresses.id |
| billing_address_id | uuid | Yes | FK → addresses.id |
| placed_at | timestamptz | No | - |
| paid_at | timestamptz | Yes | - |
| shipped_at | timestamptz | Yes | - |
| order_items | |||
| id | uuid | No | PK |
| order_id | uuid | No | FK → orders.id |
| product_id | uuid | No | FK → products.id |
| product_title | varchar | No | - |
| quantity | integer | No | - |
| unit_price_cents | integer | No | - |
| total_cents | integer | No | - |
| created_at | timestamptz | No | - |
| payments | |||
| id | uuid | No | PK |
| order_id | uuid | No | FK → orders.id |
| provider | varchar | No | - |
| provider_payment_id | text | Yes | - |
| amount_cents | integer | No | - |
| currency | varchar(3) | No | - |
| status | varchar | No | - |
| created_at | timestamptz | No | - |
| reviews | |||
| id | uuid | No | PK |
| product_id | uuid | No | FK → products.id |
| customer_id | uuid | No | FK → customers.id |
| rating | smallint | No | - |
| title | varchar | Yes | - |
| body | text | Yes | - |
| created_at | timestamptz | No | - |
15 relationships connect the tables in this schema. Each entry shows the child columns, the parent columns they reference, and the cardinality.
addresses.customer_id → customers.id
One customer can have many addresses (addresses.customer_id -> customers.id, required).
categories.parent_id → categories.id
One category can have many child categories (categories.parent_id -> categories.id, optional).
products.category_id → categories.id
One category can have many products (products.category_id -> categories.id, optional).
product_images.product_id → products.id
One product can have many product images (product_images.product_id -> products.id, required).
carts.customer_id → customers.id
One customer can have many carts (carts.customer_id -> customers.id, optional).
cart_items.cart_id → carts.id
One cart can have many cart items (cart_items.cart_id -> carts.id, required).
cart_items.product_id → products.id
One product can have many cart items (cart_items.product_id -> products.id, required).
orders.customer_id → customers.id
One customer can have many orders (orders.customer_id -> customers.id, required).
orders.shipping_address_id → addresses.id
One address can have many orders (orders.shipping_address_id -> addresses.id, optional).
orders.billing_address_id → addresses.id
One address can have many orders (orders.billing_address_id -> addresses.id, optional).
order_items.order_id → orders.id
One order can have many order items (order_items.order_id -> orders.id, required).
order_items.product_id → products.id
One product can have many order items (order_items.product_id -> products.id, required).
payments.order_id → orders.id
One order can have many payments (payments.order_id -> orders.id, required).
reviews.product_id → products.id
One product can have many reviews (reviews.product_id -> products.id, required).
reviews.customer_id → customers.id
One customer can have many reviews (reviews.customer_id -> customers.id, required).
| Relationship | Type | Meaning |
|---|---|---|
| addresses.customer_id → customers.id | One-to-many | One customer can have many addresses (addresses.customer_id -> customers.id, required). |
| categories.parent_id → categories.id | Self-referencing | One category can have many child categories (categories.parent_id -> categories.id, optional). |
| products.category_id → categories.id | One-to-many | One category can have many products (products.category_id -> categories.id, optional). |
| product_images.product_id → products.id | One-to-many | One product can have many product images (product_images.product_id -> products.id, required). |
| carts.customer_id → customers.id | One-to-many | One customer can have many carts (carts.customer_id -> customers.id, optional). |
| cart_items.cart_id → carts.id | One-to-many | One cart can have many cart items (cart_items.cart_id -> carts.id, required). |
| cart_items.product_id → products.id | One-to-many | One product can have many cart items (cart_items.product_id -> products.id, required). |
| orders.customer_id → customers.id | One-to-many | One customer can have many orders (orders.customer_id -> customers.id, required). |
| orders.shipping_address_id → addresses.id | One-to-many | One address can have many orders (orders.shipping_address_id -> addresses.id, optional). |
| orders.billing_address_id → addresses.id | One-to-many | One address can have many orders (orders.billing_address_id -> addresses.id, optional). |
| order_items.order_id → orders.id | One-to-many | One order can have many order items (order_items.order_id -> orders.id, required). |
| order_items.product_id → products.id | One-to-many | One product can have many order items (order_items.product_id -> products.id, required). |
| payments.order_id → orders.id | One-to-many | One order can have many payments (payments.order_id -> orders.id, required). |
| reviews.product_id → products.id | One-to-many | One product can have many reviews (reviews.product_id -> products.id, required). |
| reviews.customer_id → customers.id | One-to-many | One customer can have many reviews (reviews.customer_id -> customers.id, required). |
These PostgreSQL queries run against the schema shown on this page.
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;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;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;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;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;
cart_items can change; order_items never should. Copy, do not move, at checkout.categories.parent_id points at categories.id, which builds a tree such as Electronics → Phones → Cases without a separate hierarchy table.price_cents integer with a currency column.payments row per attempt preserves retries and failures instead of overwriting one status field.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 →