Type-Safe Money
Store monetary values with their currency. Arithmetic between different currencies throws errors - no silent bugs.
Type-Safe Money
Store monetary values with their currency. Arithmetic between different currencies throws errors - no silent bugs.
Real-time Exchange Rates
10 built-in sources: ECB, Bank of Canada, RBA, Swiss National Bank, and more. 7 free, 3 paid.
70+ Currencies
All major fiat currencies, precious metals (XAU, XAG), and crypto (BTC, ETH) with correct decimal precision.
Easy Conversions
Convert with arrow operator: price ->> 'USD' or macros: USD(price), EUR(price).
INSTALL monetary FROM community;LOAD monetary;-- Parse monetary valuesSELECT '99.99 USD'::monetary AS price;SELECT '$49.99'::monetary AS price; -- Symbol parsingSELECT '€100'::monetary AS price; -- Euro symbol
-- Safe arithmetic (same currency only)SELECT '100 EUR'::monetary + '50 EUR'::monetary; -- 150.00 EURSELECT '100 EUR'::monetary * 2; -- 200.00 EUR
-- This throws an error (different currencies)SELECT '100 EUR'::monetary + '50 USD'::monetary;-- Error: Cannot add monetary values with different currencies-- Fetch rates from European Central Bank (free)CREATE TABLE exchange_rates ASSELECT * FROM ecb_exchange_rates();
-- See what we gotSELECT currency_to, rateFROM exchange_ratesWHERE currency_from = 'EUR'LIMIT 5;-- Configure which table has ratesSET monetary_exchange_table = 'exchange_rates';
-- Convert using arrow operatorSELECT '100 EUR'::monetary ->> 'USD' AS in_dollars;
-- Or use currency macrosSELECT USD('100 EUR'::monetary) AS in_dollars;SELECT GBP('100 EUR'::monetary) AS in_pounds;SELECT JPY('100 EUR'::monetary) AS in_yen;-- Products priced in EUR, shown in multiple currenciesCREATE TABLE products (name VARCHAR, price monetary);INSERT INTO products VALUES ('Basic', '9.99 EUR'), ('Pro', '29.99 EUR'), ('Enterprise', '99.99 EUR');
SELECT name, price AS eur, price ->> 'USD' AS usd, price ->> 'GBP' AS gbpFROM products;-- Get currency for any countrySELECT currency_for_country('US'); -- USDSELECT currency_for_country('JP'); -- JPYSELECT currency_for_country('DE'); -- EUR
-- Get all countries using a currencySELECT countries_for_currency('EUR');-- ['DE', 'FR', 'IT', 'ES', 'NL', 'BE', 'AT', ...]| Source | API Key | Base Currency | Update |
|---|---|---|---|
| ECB | No | EUR | Daily |
| Bank of Canada | No | CAD | Daily |
| Reserve Bank of Australia | No | AUD | Daily |
| Norges Bank | No | NOK | Daily |
| Czech National Bank | No | CZK | Daily |
| Swiss National Bank | No | CHF | Daily |
| FreeGoldAPI | No | USD (Gold) | Daily |
| Twelve Data | Yes | USD | Real-time |
| Open Exchange Rates | Yes | USD | Hourly |
| GoldAPI | Yes | USD | Real-time |
Installation Guide
Exchange Rates
Currency Conversions
All Functions