DEV Community

Cover image for Building a PostgreSQL Database for E-commerce
DbVisualizer
DbVisualizer

Posted on

5

Building a PostgreSQL Database for E-commerce

Designing a database for an e-commerce platform is essential for data management. This article provides a brief guide to building a PostgreSQL database for an online store.

We will create three main tables: Items, Customers, and Orders.

Items Table

  • item_id (Primary Key)
  • item_name
  • item_category
  • item_price

Customers Table

  • customer_id (Primary Key)
  • customer_firstname
  • customer_lastname
  • customer_address

Orders Table

  • order_id (Primary Key)
  • customer_id (Foreign Key)
  • item_id (Foreign Key)
  • items_bought

Normalization reduces redundancy by creating relationships between tables.

Creating Tables

CREATE TABLE items (
  item_id SERIAL PRIMARY KEY,
  item_name VARCHAR(50),
  item_category VARCHAR(50),
  item_price INT
);

CREATE TABLE customers (
  customer_id SERIAL PRIMARY KEY,
  customer_firstname VARCHAR(50),
  customer_lastname VARCHAR(50),
  customer_address VARCHAR(100)
);

CREATE TABLE orders (
  order_id SERIAL PRIMARY KEY,
  customer_id INT REFERENCES customers(customer_id),
  item_id INT REFERENCES items(item_id),
  items_bought INT
);
Enter fullscreen mode Exit fullscreen mode

Creating Views and Importing Data

CREATE VIEW TotalOrders AS
SELECT orders.order_id, customers.customer_firstname, customers.customer_lastname, items.item_name, items.item_category, items.item_price, orders.items_bought, customers.customer_address, items.item_price * orders.items_bought AS spend
FROM orders
INNER JOIN customers ON orders.customer_id = customers.customer_id
INNER JOIN items ON orders.item_id = items.item_id;
Enter fullscreen mode Exit fullscreen mode

FAQ

How to install PostgreSQL?
Download and install PostgreSQL from its official site following the provided instructions.

What tool to use for designing the database?
QuickDBD is a recommended tool for creating database diagrams and generating SQL scripts.

How to connect Postgres to DbVisualizer?
Open DbVisualizer, create a new connection, select the Postgres driver, and fill in the necessary connection details.

Can DbVisualizer create data visualizations?
Yes, DbVisualizer supports converting table data into different chart types for better visualization.

Conclusion

Designing a PostgreSQL database for e-commerce involves creating structured tables and relationships. For more detailed steps and code examples, check out the article How To Design And Build A Database In Postgres.

Neon image

Build better on Postgres with AI-Assisted Development Practices

Compare top AI coding tools like Cursor and Windsurf with Neon's database integration. Generate synthetic data and manage databases with natural language.

Read more →

Top comments (0)

Image of Quadratic

Free AI chart generator

Upload data, describe your vision, and get Python-powered, AI-generated charts instantly.

Try Quadratic free

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay