How to Cast a Value to Another Data Type in PostgreSQL
Convert a value to another data type in PostgreSQL with :: or CAST, and see what happens when it can’t be converted.
- Database
- PostgreSQL
Convert a value to another data type in PostgreSQL with :: or CAST, and see what happens when it can’t be converted.
Write dates in PostgreSQL year first, as YYYY-MM-DD after the DATE key word, so every server reads them the same way.
Write text in PostgreSQL between single quotes, and type a single quote twice to put one inside the text.
PostgreSQL’s three whole-number types differ only in size and range. Each limit is tried live, with advice on which one to use.
A boolean holds true, false or NULL, and NULL behaves differently when you search. A small to-do list shows IS NULL and IS NOT TRUE at work.
Check the data type of every column in a PostgreSQL table with a psql command, or with an information_schema query in any tool.
Generate random and time-ordered UUIDs in PostgreSQL with gen_random_uuid() and uuidv7(), and use one as a table’s ID.
Identity columns and serial both give rows numbers that count up by themselves. Here’s why an identity column is the better choice for IDs, tried side by side.
timestamptz stores a moment and shows it in your time zone; timestamp keeps only the clock reading. One table, viewed from two time zones, shows the difference.
Store prices in PostgreSQL with numeric and two decimals, and see why the money and real types are worse choices, with real psql output.