Site icon JD Bots

How to Use the Query Tool in pgAdmin 4

Featured card for the guide on the pgAdmin 4 Query Tool, with a checklist: F5 runs everything, select and F5 runs one statement, results in Data Output and errors in Messages.

How to Use the Query Tool in pgAdmin 4

The Query Tool is where you write and run SQL in pgAdmin 4. You type in an editor, press F5, and the result appears in a grid underneath.

Every screen on this page is real, from pgAdmin 4 9.19 with PostgreSQL 19 on macOS.

Step 1: Open the Query Tool

In the Object Explorer, click the database you want to work in, then click the Query Tool button: the second icon in the toolbar above the tree, a database with a small arrow:

Select a database, then click the Query Tool button.

A new tab opens with an empty editor, connected to that database.

Step 2: Type Your SQL

Type these two statements, one on each line:

Query Tool

SELECT 'Hello, pgAdmin!' AS greeting;
SELECT 2 + 3 AS total;
Two statements, one on each line.

Step 3: Run Everything with F5

Press F5, or click the Execute script button, the triangle in the toolbar. With nothing selected, pgAdmin runs every statement in the editor, and Data Output shows the result of the last one:

F5 runs both statements; Data Output shows the last result.

Step 4: Run Only the Statement You Select

Select the first line with your mouse, then press F5 again. Only the selection runs:

Select one statement and press F5 to run only that one.

This is handy when the editor holds several statements and you want the result of just one.

Step 5: Read an Error

When a statement has a mistake, pgAdmin switches to the Messages tab to show PostgreSQL’s error, and underlines the spot in the editor. Here, SELECT is misspelt:

Query Tool

SELEC 2 + 3 AS total;
A typo: pgAdmin switches to Messages and underlines the mistake.

The caret (^) under the line and Character: 1 point to where PostgreSQL stopped understanding the statement.

Common Mistakes

MistakeWhat happensDo this instead
Expecting both results after running two statementsData Output shows only the result of the last one.Select one statement and press F5 to see its result.
A misspelt keyword, such as SELECERROR: syntax error at or near "SELEC"Fix the word the caret points to, then run it again.

FAQ

What happens to my SQL when I close the tab?

If the text has changed, pgAdmin asks first:

Closing a Query Tool with unsaved SQL asks first.

Click Don’t save to close the tab without keeping the SQL, or Cancel to go back to it.

Which key runs a query?

F5 runs the script, as pgAdmin’s list of keyboard shortcuts shows.

Conclusion

Open the Query Tool on a database, type your SQL and press F5. With nothing selected it runs everything and shows the last result; select one statement to run just that, and read any errors in the Messages tab.

Additional Resources

Related

Postgres, PostgreSQL and the Slonik Logo are trademarks or registered trademarks of the PostgreSQL Community Association of Canada, and used with their permission.

Exit mobile version