Skip to main content

Postgres Playground

Run PostgreSQL queries in your browser on a real Postgres engine, with a schema list, EXPLAIN, several SQL scripts and a zip download.

-- Press Run (or Ctrl+Enter) to run this file. Every statement gets its own result table.

SELECT version();

-- Revenue of paid orders per country
SELECT c.country, count(*) AS orders, sum(o.amount) AS revenue
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.status = 'paid'
GROUP BY c.country
ORDER BY revenue DESC;

-- generate_series makes rows out of nothing
SELECT n, n * n AS square FROM generate_series(1, 5) AS n;

Results

Press Run to see the result tables here. The first run loads the Postgres engine (a few MB, once).

Shortcuts: Ctrl+Enter run · / search (use ⌘ on Mac)

Free Online PostgreSQL Playground

Try PostgreSQL without installing anything. A real Postgres engine runs inside this page, with two practice tables already filled (50 customers and 150 orders), so you can test a query, learn window functions or check an idea before you use it on a real server. Your SQL never leaves your device. It runs PostgreSQL compiled for the browser, so window functions, CTEs and JSON work as on a server.

How to use the playground

  1. Press Run (or Ctrl+Enter). The first time, the engine downloads once and a progress bar shows while it loads.
  2. Edit the SQL in the open tab, or press + to add another script file. Run all runs every file in the order of the tabs.
  3. Each statement gets its own result table with the row count and time. Select text before pressing Run to run only that part.
  4. Click a table in Schema to add a quick SELECT for it. Explain shows the plan of the last statement with real timings.
  5. In the ⋯ menu, download a .sql file of what you changed, reset the database, or open a .sql script from your computer.

Good to know

The database lives in memory and starts fresh each visit. Table and column names are lower case unless you wrap them in double quotes. To tidy a long query, use the SQL Formatter; to try the same ideas on a single file database, open the SQLite Playground.

Worked example

This query adds up the paid orders for each country:

SELECT c.country, count(*) AS orders, sum(o.amount) AS revenue
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.status = 'paid'
GROUP BY c.country
ORDER BY revenue DESC;

The result has six rows. The first three are:

country  | orders | revenue
---------+--------+---------
UK       | 15     | 4198.60
Spain    | 16     | 3928.00
Portugal | 11     | 3025.70

Common mistakes

  • Typing \d or \dt. Those are psql commands; use the Schema list or information_schema.
  • Using a capital letter in a table name without quotes. CREATE TABLE Users makes a table called users.
  • Comparing text and numbers without a cast. Write '5'::int when a function complains about its inputs.
  • Hoping the tables are still there next visit. The database is only in memory; download the session SQL to keep it.

Frequently asked questions

Is this really PostgreSQL?
Yes. The engine is PGlite, the real PostgreSQL server code compiled to run inside the browser. It supports things such as window functions, CTEs, JSON, arrays and generate_series. Only a few add-on modules are included, and there is no network access to other databases.
Why does the first run take a moment?
The Postgres engine is a few megabytes and is downloaded from this site the first time you press Run, with a progress bar. Your browser keeps it, so later visits start faster. Nothing you type is sent anywhere.
Do my tables stay after I close the page?
No. The database is kept in memory only. Use the ⋯ menu to download a .sql file with the statements that changed something in this session, and run that file again later to rebuild the tables.
Why does \d say it is not supported?
Commands that start with a backslash belong to psql, the command-line program, not to SQL. Use the Schema list on the right, or ask for the same facts with SELECT table_name FROM information_schema.tables.
What does Explain do?
It puts EXPLAIN (ANALYZE) in front of the last statement (or the text you selected) and shows the plan with real timings. Because ANALYZE runs the statement, an INSERT or DELETE inside it changes the data.
Why did my query stop after 10 seconds?
A query that never ends would freeze the page, so it is stopped after a time limit. The database is then started again and the statements you ran before are replayed, which takes a few seconds. You can raise the limit in the ⋯ menu.
Can I connect it to my own Postgres server?
No. It is a separate in-memory database inside your browser and makes no network connections, which is why nothing you type can reach a real server.

Related tools

More Playground

View all Playground tools →