← All bricks

psql · Beginner · Free

psql: The PostgreSQL Client

Get fluent in psql — connect, create a database, run and save queries, format and export results, and make the client your own.

Most people meet PostgreSQL through a GUI and never learn the tool that ships with it. psql is faster, scriptable, and always there on the server you just SSH'd into. We start from an empty prompt and build up real fluency: creating a database, exploring it with meta-commands, keeping schema and queries in files you can replay, passing values in with variables, exporting results, and finally shaping the client to your taste with a startup file. Small steps, each one a thing you'll use the same day.

What you'll build

  • Connect to PostgreSQL and move around with meta-commands
  • Create a database and load schema and data from files
  • Read, format and export query results
  • Parameterise saved queries with psql variables
  • Run psql non-interactively in scripts
  • Customise the client with a .psqlrc

Contents

  1. Open a session
  2. Create a database
  3. Find your way around
  4. Keep your schema in a file
  5. Run a file from inside the session
  6. Give it some data
  7. Read a result
  8. Tame wide output
  9. Save a query you'll run again
  10. Pass values in
  11. Get results out
  12. Inspect what's there
  13. Use it from a script
  14. Compose a repeatable report
  15. Make the client yours