Skip to content
AccountancyPath

Accounting for DevelopersModule 2: Data model and designAdvanced2 min read

Journal and General Ledger Database Design

Design journal entry and posting tables for a general ledger: schema, constraints, effective vs created dates, balance queries, trial balance SQL and scaling.

Lesson 4 of 16
Course outline (16 lessons)

Before this lesson:Chart of Accounts Database Design for DevelopersGeneral Ledger

In accounting terms, the journal is the list of entries and the general ledger is those entries organised by account. In a database, both are usually the same two tables viewed differently.

The schema

CREATE TABLE journal_entries (
  id               uuid PRIMARY KEY,
  entity_id        uuid NOT NULL REFERENCES entities(id),
  idempotency_key  text NOT NULL,
  effective_date   date NOT NULL,          -- accounting date: which period it belongs to
  created_at       timestamptz NOT NULL DEFAULT now(),
  created_by       text NOT NULL,          -- user or service
  source_type      text NOT NULL,          -- 'invoice', 'payment', 'manual', 'reversal'...
  source_id        text,                   -- id in the originating system
  description      text NOT NULL,
  reverses_entry_id uuid REFERENCES journal_entries(id),
  UNIQUE (entity_id, idempotency_key)
);

CREATE TABLE postings (
  id            bigserial PRIMARY KEY,
  entry_id      uuid NOT NULL REFERENCES journal_entries(id),
  account_id    uuid NOT NULL REFERENCES accounts(id),
  amount_minor  bigint NOT NULL CHECK (amount_minor <> 0), -- debit > 0, credit < 0
  currency      char(3) NOT NULL,
  counterparty_id uuid,
  memo          text
);

CREATE INDEX postings_account_idx ON postings (account_id, entry_id);

No UPDATE or DELETE should ever run against these tables in normal operation. Revoke those privileges from the application role.

Enforcing balance in the database

Application validation is not enough if more than one service writes to the ledger. Options include:

  • a deferred constraint trigger that, at commit, checks SUM(amount_minor) = 0 per entry and currency;
  • a single stored procedure that is the only permitted way to insert an entry and its postings.

Effective date vs created timestamp

  • Effective date determines the accounting period. A March accrual entered on 2 April has an effective date of 31 March.
  • Created timestamp records when it really happened.

Reports “as at” a date use effective dates. Audit queries (“what did the March report show on 1 April?”) need created timestamps too.

Period locking

Once a month is closed and reported, nothing should change it. Keep a periods table with a status, and reject entries whose effective date falls in a closed period. Corrections go into the current open period, with a description referencing the original.

Querying balances

-- Account balances as at a date (debit-positive convention)
SELECT p.account_id, p.currency, SUM(p.amount_minor) AS balance_minor
FROM postings p
JOIN journal_entries e ON e.id = p.entry_id
WHERE e.entity_id = :entity AND e.effective_date <= :as_of
GROUP BY p.account_id, p.currency;

Trial balance

SELECT a.code, a.name,
       GREATEST(SUM(p.amount_minor), 0)  AS debit_minor,
       GREATEST(-SUM(p.amount_minor), 0) AS credit_minor
FROM postings p
JOIN journal_entries e ON e.id = p.entry_id
JOIN accounts a ON a.id = p.account_id
WHERE e.entity_id = :entity AND e.effective_date <= :as_of AND p.currency = :currency
GROUP BY a.code, a.name
HAVING SUM(p.amount_minor) <> 0
ORDER BY a.code;

The two column totals must be equal. If they are not, the ledger’s invariant has been broken somewhere, and that is a serious incident. See the trial balance lesson for the accounting concepts, and building a trial balance from ledger data for period filtering, normal-balance checks, TypeScript aggregation and snapshots.

Scaling

  • Cached balances: a account_balances table updated in the same transaction as postings, with row-level locking per account. Hot accounts (such as a platform cash account) can become contention points; techniques include sharding the hot account into several sub-accounts or batching postings.
  • Periodic snapshots: store closing balances per account per period, so queries only sum postings since the last snapshot.
  • Verification jobs: regularly recompute balances from postings and alert on any difference.

Recap

  • Journal entries are headers; postings are immutable lines that sum to zero.
  • Enforce balance in the database as well as in code.
  • Separate effective dates from created timestamps, and lock closed periods.
  • Balances and the trial balance are queries over postings; caches must be verifiable.

Active recall

Quick check · Test yourself

Work out each answer in your head first, then reveal the explanation and mark whether you got it right.

  1. 1Why keep both an effective date and a created timestamp on a journal entry?

    Show worked answer

    The effective date decides which accounting period the entry belongs to. The created timestamp records when it was actually entered. They differ for back-dated adjustments, and auditors need both.

  2. 2How would you produce a trial balance as at 31 March from the posting table?

    Show worked answer

    Sum postings per account for entries with an effective date on or before 31 March, then split positive sums into the debit column and negative sums into the credit column.

Key terms in this lesson

Lesson 4 of 16 · Accounting for Developers

Finished this lesson?

Mark it complete to track your progress through the course. Progress is saved in this browser only.

Previous lesson: Chart of Accounts Database Design for Developers

Subject GuideAccounting Systems & Personal Finance

Mei-Ling Chen(陈美玲)

Turns accounting invariants into clean code—and everyday personal money clarity.

  • Double-Entry Ledger Engineering
  • SQL Schemas & Financial APIs
  • Multi-Currency & Billing Code

More lessons with Mei-Ling

Published:

Educational content only—not accounting, tax, legal or investment advice. See our editorial policy or report an error via our corrections policy.

Search