← Back to PostgreSQL Course | Chapter 1: Introduction | Lesson 4 of 7

psql basics

psql is PostgreSQL's command-line client: you type SQL and shortcuts beginning with a backslash.

In this page:

  1. psql basics
Syntax
sql
psql -U username -d database_name -h host
\l
\c database_name
\dt
\d table_name
\q

psql basics

Connect with psql -U user -d database -h host. Useful meta-commands are \l (list databases), \c name (connect), \dt (list tables), \d table (describe a table), \du (roles), \x (expanded display), \i file.sql (run a file), \timing and \q (quit).

SQL statements end with a semicolon.

Note: Type \? for the full list of psql commands.

Example: psql basics

bash
$ psql -U postgres
postgres=# \l
postgres=# \c shop
You are now connected to database "shop" as user "postgres".
shop=# \dt
        List of relations
 Schema |   Name    | Type  |  Owner
--------+-----------+-------+----------
 public | customers | table | postgres
shop=# \d customers
shop=# \q

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
Common Mistakes
  1. Forgetting the semicolon at the end of SQL
  2. Using SQL for meta-commands
  3. Typing \d without a table name and getting a long list
Chapter Summary
  • psql is the interactive client
  • Meta-commands start with a backslash
  • \l \c \dt \d are the basics
  • SQL ends with a semicolon
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.