ReviseAlgo Logo

Setting Up PostgreSQL

pgAdmin and psql

Using pgAdmin graphical console and psql command line tool.

Interview: Backend engineers must be fluent in psql for production debugging, log analysis, and emergency fixes — interviewers often test your ability to navigate psql efficiently without a GUI.

Last Updated: June 12, 2026 18 min read

1. Introduction

PostgreSQL provides two primary interfaces for database interaction: pgAdmin (a graphical web-based tool) and psql (a powerful command-line interface). While pgAdmin is beginner-friendly, psql is the professional standard for production database management.

2. Why It Matters

Backend engineers must be fluent in psql for production debugging, log analysis, and emergency fixes. During incidents, you'll SSH into a server with no GUI — only psql. Interviewers test your ability to navigate psql efficiently and use meta-commands to inspect schemas, time queries, and export data.

3. Real-World Analogy

psql is like a terminal multiplexer (tmux/vim) — fast, scriptable, and available everywhere. pgAdmin is like a file manager GUI — visual, intuitive, but not available over SSH. A professional developer uses both: pgAdmin for schema exploration and ERD visualization, psql for automation and production access.

4. How It Works

Essential psql meta-commands (prefixed with backslash):

  • \l — List all databases
  • \c dbname — Connect to a specific database
  • \dt — List all tables in current database
  • \d table_name — Describe table structure (columns, types, constraints, indexes)
  • \du — List all database users/roles
  • \timing — Toggle query execution timing
  • \x — Toggle expanded display (one column per line)
  • \q — Quit psql

5. Internal Architecture

psql connects to PostgreSQL via a TCP socket or Unix domain socket. It sends SQL as text, receives results as text, and renders them in a tabular format. pgAdmin is a web application (Python/Flask backend + JavaScript frontend) that runs in your browser and connects to PostgreSQL over TCP.

6. Visual Explanation

7. Practical Example

A typical psql session for schema exploration:

Exporting data to CSV via psql:

8. Common Mistakes

Common Pitfall

Relying solely on pgAdmin and never learning psql. In production environments, you'll often only have SSH access — no GUI. You must be comfortable using psql for emergency fixes, log analysis, and data migrations.

  • Missing semicolons: psql requires SQL statements to end with a semicolon. Meta-commands (d, l) do not need one.
  • Not enabling iming: Without execution time visibility, you cannot identify slow queries during development.
  • Using pgAdmin in production: Production databases should only be accessed via psql over SSH tunnels, never by exposing pgAdmin to the public internet.

9. Quick Quiz

Q1: Which psql command shows a table's indexes and constraints?

d table_name displays columns, types, modifiers, and indexes. Use d+ for additional details like storage parameters and comments.

Q2: How do you run a SQL file non-interactively?

psql -U user -d database -f script.sql executes the file and exits. Useful for migrations and CI/CD pipelines.

10. Scenario-Based Challenge

Scenario

A production database is returning slow queries. You SSH into the server and have only psql. How would you identify which queries are currently running, how long they've been executing, and whether there's lock contention? Hint: look at pg_stat_activity.

11. Debugging Exercise

You run a query but psql shows no output and hangs:

Fix: Missing semicolon. psql shows mydb-# (with a dash) indicating it's waiting for the statement to complete. Add a semicolon to execute: ...'%@example.com';

12. Interview Questions

Q: How would you check what queries are currently running on a PostgreSQL server?

Query pg_stat_activity: SELECT pid, usename, state, query, now() - query_start AS duration FROM pg_stat_activity WHERE state = 'active';

Q: What's the advantage of \x (expanded display)?

When a table has many columns, normal tabular output wraps and becomes unreadable. \x displays one column per line, making wide tables easy to read.

Q: When would you use pgAdmin over psql?

pgAdmin excels at visual tasks: browsing schemas with a tree view, generating ERD diagrams, monitoring dashboards, and building complex queries with auto-completion for beginners.

13. Production Considerations

  • psql for automation: Use psql in CI/CD pipelines for migrations, seed data loading, and health checks. Script it with -f (file) or -c (command) flags.
  • Connection pooling: In production, use PgBouncer or Pgpool-II between your application and PostgreSQL. Direct connections from every app instance waste resources.
  • pgAdmin security: If using pgAdmin remotely, always tunnel through SSH. Never expose pgAdmin's web interface to the public internet.
  • .psqlrc configuration: Customize psql behavior with a .psqlrc file (auto-enable iming, set output format, configure prompts with host and database name).

Use Cases

Production debugging: Using psql via SSH to investigate slow queries, check active connections, and examine lock contention in real-time

Data migration: Running SQL scripts via psql to migrate data between tables or databases during deployment

Schema exploration: Using \d and \dt meta-commands to quickly understand an unfamiliar database schema

Common Mistakes

Not enabling \timing during query development — without execution time visibility, you cannot identify slow queries that will cause production issues

Using pgAdmin for production access — production databases should only be accessed via psql over SSH tunnels, never by exposing pgAdmin to the public internet

Forgetting the semicolon — psql requires SQL statements to end with a semicolon. Meta-commands (\d, \l) do not need one. Missing semicolons are the most common beginner error