Back to Blog
Lesson 29 of the PostgreSQL: SQL & PostgreSQL from Scratch course
DatabasesAugust 17, 20264 min read

Establishing Naming Conventions: Best Practices for PostgreSQL

Learn professional naming conventions for PostgreSQL to keep your database readable. Discover snake_case, consistent key naming, and schema design standards.

PostgreSQLSQLNaming ConventionsBest PracticesDatabase Design
Hands organizing conference programs and name tags at event registration desk.

Previously in this course, we explored handling timestamps. While that lesson focused on managing temporal data, today we pivot to the "architecture of readability": establishing naming conventions that make your schema a pleasure to work with, rather than a cryptic puzzle.

In production environments, a database schema is documentation in itself. If your naming is inconsistent, you'll eventually encounter technical debt that slows down development and increases the risk of bugs.

Why Naming Conventions Matter

In PostgreSQL, identifiers (table names, column names) are case-insensitive by default unless you wrap them in double quotes. This causes common headaches: UserAccount might be treated as useraccount, while "UserAccount" is treated literally. To avoid these traps, the industry standard is to use snake_case for everything.

The Core Principles

  1. Consistency: If you name a primary key id in one table, name it id in all tables.
  2. Clarity: Avoid abbreviations that require a decoder ring (e.g., use customer_email instead of cust_eml).
  3. Plural vs. Singular: Use singular nouns for table names (customer instead of customers) to align with the concept that a table represents a single entity definition.

Implementing Consistent Key Naming

Foreign keys are the glue of your relational model. If your naming is inconsistent, your JOIN queries will become difficult to read.

  • Primary Keys: Always use id. It is simple, standard, and works perfectly with PostgreSQL’s SERIAL or UUID types.
  • Foreign Keys: Use the pattern [table_name]_id. For example, if you are referencing the customer table from an order table, the column should be customer_id.

Worked Example: Refactoring the Store Schema

Let's look at how we apply these standards to our running store project. Imagine we are cleaning up a messy schema:

SQL
-- BAD: Inconsistent and mixed case
CREATE TABLE CustomerData (
    CustID INT PRIMARY KEY,
    User_Name TEXT,
    emailAddress TEXT
);

CREATE TABLE Orders (
    oid INT PRIMARY KEY,
    c_id INT REFERENCES CustomerData(CustID)
);

-- GOOD: Clean, consistent, and readable
CREATE TABLE customer (
    id INT PRIMARY KEY,
    username TEXT NOT NULL,
    email_address TEXT NOT NULL
);

CREATE TABLE "order" (
    id INT PRIMARY KEY,
    customer_id INT REFERENCES customer(id)
);

Note: order is a reserved keyword in SQL. If you must use it as a table name, you must quote it, but it is often better to use store_order to avoid needing quotes entirely.

Best Practices for Schema Design

Beyond simple casing, here is how you ensure your database remains professional as it grows:

ElementConventionExample
Table NamesLowercase snake_caseproduct_category
Column NamesLowercase snake_casecreated_at
Primary Keysidid
Foreign Keys[table]_idproduct_id
Boolean Flagsis_[state]is_active

Hands-on Exercise

Open your PostgreSQL terminal or pgAdmin query tool. Review the customers and products tables you created in earlier lessons.

  1. Write an ALTER TABLE statement (or recreate the table) to rename any non-compliant columns to snake_case.
  2. Ensure all foreign key columns end in _id.
  3. Rename any ambiguous columns (like data) to descriptive names (like customer_bio).

Common Pitfalls

  • Reserved Keywords: Never name a column user, order, or date. PostgreSQL will fight you on these. Use app_user or order_date instead.
  • Over-abbreviation: p_id is ambiguous. Is it product_id or payment_id? Always favor explicit, full words over brevity.
  • Quoting Identifiers: Avoid using double quotes for identifiers at all costs. It forces you to use quotes every time you query the table, which is a massive productivity killer. Stick to lowercase letters and underscores.

FAQ

Q: Should I use id or [table]_id for the primary key? A: Use id for the primary key of the table itself. Use [table]_id only when that ID appears in another table as a foreign key.

Q: Is it okay to use camelCase? A: While some teams do, PostgreSQL converts unquoted identifiers to lowercase. If you use userName, it will be stored as username. Mixing case styles leads to confusion; stick to snake_case for consistency.

Recap

We’ve established that professional naming is not just about aesthetics—it’s about preventing errors and reducing cognitive load. By enforcing snake_case, using explicit names, and standardizing how we link primary and foreign keys, we’ve made our store schema robust and maintainable.

Up next: Database Documentation Standards — where we will learn how to use comments to make our schema self-documenting.

Similar Posts