For Developers
How to get and convert Unix timestamps in SQL — with the actual syntax differences between PostgreSQL, MySQL, and SQLite side by side, since each one does this differently.
-- PostgreSQL
SELECT EXTRACT(EPOCH FROM NOW());
-- MySQL
SELECT UNIX_TIMESTAMP();
-- SQLite
SELECT strftime('%s', 'now');
-- PostgreSQL
SELECT TO_TIMESTAMP(1750000000);
-- MySQL
SELECT FROM_UNIXTIME(1750000000);
-- SQLite
SELECT datetime(1750000000, 'unixepoch');
-- PostgreSQL
SELECT EXTRACT(EPOCH FROM TIMESTAMP '2025-06-15 12:00:00');
-- MySQL
SELECT UNIX_TIMESTAMP('2025-06-15 12:00:00');
-- SQLite
SELECT strftime('%s', '2025-06-15 12:00:00');
There is no standard SQL function for this — every engine invented its own, and the return types don't match. PostgreSQL's EXTRACT(EPOCH FROM ...) returns a double precision (fractional seconds included), MySQL's UNIX_TIMESTAMP() returns an integer or decimal depending on the input, and SQLite's strftime('%s', ...) returns a string that many drivers won't auto-cast for you in a WHERE clause comparison. If a query moves between engines — migrations, ORMs targeting multiple databases, or copy-pasted Stack Overflow snippets — check the return type explicitly rather than assuming it behaves like the last engine you used.
Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.
Related
Every language's snippets side by side, for quick comparison.
Convert any timestamp instantly, no code required.
Column types and pitfalls when timestamps live in a database.