Sub topic 13
Running Queries in the pgAdmin Query Tool
Assume you know basic SELECT, WHERE, JOIN, and ORDER BY statements. You also need access to a PostgreSQL server and permission to connect to the database you want to inspect. The pgAdmin Query Tool gives you a SQL editor, execution controls, result grids, messages, and query-plan views in one window.
Opening the Query Tool
In the pgAdmin object browser, expand Servers, the registered server, and Databases. Right-click the target database and choose Query Tool. Opening it from a database matters: the tool runs statements in that database and uses your selected connection.
The main areas are:
- The query editor, where you write SQL.
- The toolbar, which contains execute, stop, and explain controls.
- The Data Output tab, which displays rows returned by a query.
- The Messages tab, which displays errors, notices, row counts, and execution information.
- The Query History area, which can help you find recently executed statements.
You can run the whole editor contents or select one statement and run only that selection. Selecting a statement is useful when an editor contains several exploratory queries.
SELECT column_name, data_type, is_nullable
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'orders'
ORDER BY ordinal_position;Place the cursor in this query or select it, then choose the execute button. The result appears in Data Output. If the query fails, read Messages first. PostgreSQL usually reports the error type and the approximate location of the problem.
A safe exploration workflow
Use small, read-only queries while learning an unfamiliar database. Start by checking the connection and the object names you intend to use. Add a limit when inspecting table data.
SELECT *
FROM public.orders
ORDER BY order_id
LIMIT 20;The Query Tool can also execute data-changing statements such as UPDATE and DELETE. Treat the execute button as a real database operation, not as a preview. Before changing data, verify the WHERE condition with a SELECT. For important work, use an explicit transaction and check the result before committing.
BEGIN;
UPDATE public.orders
SET status = 'shipped'
WHERE order_id = 42;
SELECT order_id, status
FROM public.orders
WHERE order_id = 42;
ROLLBACK;ROLLBACK cancels the change. Replace it with COMMIT only after you have verified the result and intend to keep it.
EXPLAIN in the Query Tool
For a SELECT, use the Explain control to ask PostgreSQL how it expects to run the query. The plan appears in an Explain tab and commonly shows scans, joins, estimated rows, and cost. The next chapter examines these plans in detail. For now, compare the plan with your query: a filter on an indexed column may use an index scan, while a small table may use a sequential scan because reading the whole table is cheaper.
> callout note > Avoid running EXPLAIN ANALYZE on data-changing statements unless you understand its effects. EXPLAIN ANALYZE executes the statement while measuring it.
Exercise
Write a Query Tool statement that lists the names and data types of columns in public.orders, in column order.
Show answer
SELECT columnname, datatype FROM informationschema.columns WHERE tableschema = 'public' AND tablename = 'orders' ORDER BY ordinalposition;