Stripe Invoices SQL Template

SQL table creation template for syncing Stripe invoice data to your PostgreSQL database.

Updated: 15 Dec 2025

Stripe Invoices SQL Template

Create a database table to store Stripe invoice data including billing details, payment status, and amounts.

What Data is Synced?

The Stripe Invoices sync captures essential invoice information from your Stripe account:

  • Invoice ID: Unique Stripe invoice identifier
  • Customer Reference: Which customer the invoice belongs to
  • Description: Optional invoice description or notes
  • Status Information: Payment status (draft, open, paid, void, uncollectible)
  • Amount Details: Total invoice amount, amount paid, and amount due
  • Currency: Three-letter currency code (e.g., usd, gbp, eur)
  • Livemode Flag: Whether this is a live or test mode invoice
  • Timestamps: When the invoice was created in Stripe
  • Complete Invoice Data: Full Stripe invoice object stored as JSONB
  • Sync Tracking: Timestamp of when CLS last synced this record

SQL Table Template

Table Schema Explanation

Here's what each column in the table represents:

ColumnTypeDescription
idTEXTStripe invoice ID (e.g., in_abc123). Primary key.
customerTEXTStripe customer ID this invoice belongs to (e.g., cus_xyz789). Required.
descriptionTEXTOptional invoice description or notes.
statusTEXTInvoice status: draft, open, paid, void, or uncollectible. Required.
totalINTEGERTotal invoice amount (in cents).
amount_paidINTEGERAmount that has been paid (in cents).
amount_dueINTEGERAmount remaining to be paid (in cents).
currencyTEXTThree-letter ISO currency code (e.g., usd, gbp, eur).
dataJSONBComplete Stripe invoice object stored as JSON.
livemodeBOOLEANWhether this is a live mode invoice (true) or test mode (false). Required.
createdTIMESTAMPTZTimestamp when invoice was created in Stripe.
synced_atTIMESTAMPTZTimestamp when CLS last synced this invoice record. Auto-updated.

All amount fields (total, amount_paid, amount_due) are stored in cents (or smallest currency unit). For example, $10.50 USD is stored as 1050. Divide by 100 to get the dollar amount.

Usage Examples

After syncing, you can query your invoice data using standard SQL:

-- Get all paid invoices
SELECT * FROM stripe_invoices
WHERE status = 'paid';

-- Find open invoices created in the last 30 days
SELECT * FROM stripe_invoices
WHERE status = 'open'
  AND created > NOW() - INTERVAL '30 days';

-- Calculate total revenue from paid invoices by currency
SELECT
  currency,
  SUM(amount_paid) / 100.0 AS total_revenue
FROM stripe_invoices
WHERE status = 'paid'
GROUP BY currency;

-- Get invoices for a specific customer
SELECT * FROM stripe_invoices
WHERE customer = 'cus_abc123'
ORDER BY created DESC;

-- Find high-value unpaid invoices (over $1000)
SELECT
  id,
  customer,
  amount_due / 100.0 AS amount_due_dollars,
  created
FROM stripe_invoices
WHERE status = 'open'
  AND amount_due > 100000;

-- Query invoices by custom data field
SELECT * FROM stripe_invoices
WHERE data->>'invoice_pdf' IS NOT NULL;

Common Customizations

The template includes performance indexes for customer, status, and livemode columns. You can add additional table-specific customizations:

Add Index for Date Range Queries

CREATE INDEX idx_stripe_invoices_created
ON stripe_invoices(created);

Add Index for Currency Filtering

CREATE INDEX idx_stripe_invoices_currency
ON stripe_invoices(currency);

Add Compound Index for Status and Amount

CREATE INDEX idx_stripe_invoices_status_amount
ON stripe_invoices(status, amount_due);