Sub topic 17
When to use psql and when to use pgAdmin
This chapter assumes you have worked through the earlier chapters. You should be able to connect with psql, use meta-commands such as \d+ and \dt, query information_schema and pg_catalog, register a server in pgAdmin 4, browse its object tree, and run a query in the Query Tool. Here you compare the two tools and pick one for a given job.
Two clients, one server
psql and pgAdmin do not differ in what they can reach. Both open a connection to the same server, send SQL over the same protocol, and see the same databases, schemas and privileges. Anything you can learn about a database in one tool, you can learn in the other. What differs is how each tool presents the work and what it asks of you.
- psql is a text-mode client. You type commands, and it prints text. Its meta-commands (the backslash commands) are shortcuts that psql turns into catalog queries for you.
- pgAdmin 4 is a graphical client. You click through an object tree, edit SQL in tabs, and read results in grids, charts and diagrams.
Choosing between them is a question of fit: where you are working, how often you will repeat the task, and whether the answer is easier to read as text or as a picture.
What each tool does best
Strengths of psql
- Remote and minimal environments. If you are on a server over SSH with no desktop, psql is usually already installed. pgAdmin needs a browser-capable environment or a separate machine that can reach the database.
- Scripting and repeatability.
psql -X -d mydb -f inspect.sqlruns the same way on every machine. You can put it in a shell script, a cron job or a CI step, and the exit status tells the caller whether it worked. - Speed for small questions.
\conninfo,\dt public.*and\d+ ordersanswer a question in one line with no mouse work. - Output you can pipe. Results go to standard output, so you can send them to
grep,less, a file or another program. - Low overhead. It starts instantly and uses very little memory.
Strengths of pgAdmin
- Browsing an unfamiliar database. The tree shows servers, databases, schemas, tables, columns, indexes and functions at once. You can expand and collapse them instead of remembering meta-commands.
- Reading wide or large results. A grid with sortable columns handles a wide row better than text that wraps across the terminal.
- Properties at a glance. The Properties, SQL and Statistics tabs for a selected object show details that would take several psql commands.
- Visual aids. The Dashboard graphs sessions and transactions. The graphical EXPLAIN view shows a plan as nodes. The ERD tool draws tables and foreign keys.
- Managing many servers. Registered servers stay in one tree, with saved connection settings.
Comparing them on the same task
The table lists common exploration tasks and the way each tool handles them.
| Task | psql | pgAdmin |
|---|---|---|
| Check which database and user you are connected as | `\conninfo` | Shown in the tree and in the Query Tool tab title |
| See all tables in a schema | `\dt public.*` | Expand Schemas, then the schema, then Tables |
| Inspect one table's columns, indexes and constraints | `\d+ orders` | Select the table and use Properties, then the SQL tab |
| Find the ten largest tables | A catalog query using `pg_total_relation_size` | Statistics tab, or the same catalog query in the Query Tool |
| Read a query plan | `EXPLAIN (ANALYZE, BUFFERS)` as text | The same command, with a graphical view of the nodes |
| Run a saved script on a schedule | `psql -X -f script.sql` | Not suited to unattended runs |
| See how tables relate through foreign keys | Read the constraints in `\d+` output | ERD tool |
| Watch current activity on the server | Query `pg_stat_activity` | Dashboard |
| Work over SSH on a remote host | Works directly | Needs a network path from the machine running pgAdmin |
Two rows deserve a closer look.
Query plans. The text plan from psql is complete and easy to paste into a bug report or a chat with a colleague. The graphical plan in pgAdmin makes it easier to spot the most expensive node at a glance, because the nodes are drawn with their relative cost. Use text when you need to share or diff a plan, and the graph when you are looking for the slow step.
Catalog queries. These work the same in both tools because they are plain SQL. Only the way the results are shown differs. A query such as the following runs in either tool.
SELECT n.nspname AS schema_name,
c.relname AS table_name,
pg_size_pretty(pg_total_relation_size(c.oid)) AS total_size
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind = 'r'
AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY pg_total_relation_size(c.oid) DESC
LIMIT 10;A decision flow
Start with where you are working and whether the task will be repeated.
Edge cases and common mistakes
Treating the two tools as exclusive
Many people use both in one session. A common pattern is to browse in pgAdmin to find the table you care about, then copy its SQL from the SQL tab and test a change in psql. Another is to run a long-running diagnostic script from psql and then open the Dashboard to see its effect on the server.
Assuming the settings carry over
Settings do not transfer between the tools. The ~/.psqlrc file applies only to psql. Settings such as \x auto, \timing on and \pset null '(null)' have no effect in pgAdmin. pgAdmin has its own preferences, and it displays NULL in its own way. If a result looks different between tools, check the client settings before you suspect the data.
Forgetting that EXPLAIN ANALYZE runs the statement
This holds in both tools. EXPLAIN ANALYZE executes the query. For UPDATE, DELETE and INSERT, wrap it in a transaction and roll back, as shown here.
BEGIN;
EXPLAIN (ANALYZE, BUFFERS)
SELECT * FROM orders WHERE customer_id = 42;
ROLLBACK;For a plain SELECT the risk is only load. For a data-changing statement the changes persist unless you roll back. Also check the Query Tool's transaction mode. If it commits automatically, a ROLLBACK you forgot can not undo anything that has already run.
Using a GUI on a production server without thinking
Browsing is not free. Expanding a large tree, opening a table's Statistics tab or running SELECT * from the "View Data" option all send queries to the server. On a busy production database, prefer targeted catalog queries and LIMIT. Use a read-only role for exploration so a stray click cannot change data.
Relying on the picture alone
An ERD draws the relationships that exist as foreign key constraints. If a database links tables by convention only, with no constraints, the diagram will not show those links. Check with a catalog query or \d+ when the picture seems to be missing something.
Practical recommendations
- First look at an unfamiliar database: use pgAdmin to get the layout, then use psql meta-commands to confirm details.
- Anything you will run twice: write it as a script and run it with
psql -X -f. - Remote servers and containers: use psql.
- Explaining a design to others: use the ERD and share the picture.
- Investigating a slow query: capture the text plan in psql for the record, and use the graphical plan to find the costly node.
- Work on production: use a role with limited privileges in either tool.
Exercise
You have SSH access to a production host and must check, every night, that the ten largest tables have not grown beyond an expected size, and send the result to a log file. Which tool do you choose, and which options keep the script consistent across machines?
Show answer
Use psql. Put the size query (using pg_total_relation_size and pg_size_pretty) in a file and run it with psql -X -d mydb -f check_sizes.sql >> sizes.log from a scheduled job. The -X option skips the psqlrc file so the output does not depend on the local settings of whoever runs it. pgAdmin is interactive and is not suited to unattended runs.
Exercise
A colleague has joined your team and must learn how 40 tables in an unfamiliar database connect to each other. Which tool is a good first choice, and what limit should you tell them about?
Show answer
pgAdmin's ERD tool (together with the object tree) gives a good first picture of the tables and their foreign keys. The limit is that it shows only relationships defined as foreign key constraints. If tables are linked by naming convention without constraints, the diagram will not show it, so they should confirm with \d+ or a catalog query.
Exercise
You run \timing on and \x auto in psql, then run the same query in the pgAdmin Query Tool and see different formatting. Is something wrong with the query?
Show answer
No. Those settings are psql client settings, so they do not apply to pgAdmin. The data and the query are the same. If the results differ in content, compare the connections (database, user, search_path) instead.