PostgreSQL - isel-leic-ls/2122-2-common GitHub Wiki

Introduction

Some notes on using PostgreSQL and the psql command line interface.

PostgreSQL

  • psql is a command line tool that can be used to:

    • Connect to a PostgreSQL server.
    • Execute SQL commands on that server.
    • Execute scripts from the command line.
  • Use psql <database> -h <host> -U <username> to create an interactive shell.

    • The password can be defined on the PGPASSWORD environment variable.
    • The user name can be defined on the PGUSER environment variable.
  • Use `psql -f <script_file> to execute a script.

  • The following commands are available inside the psql interactive shell.

    • \l lists all databases.
    • \c connects to a database.
    • \d lists all relations (e.g. tables).
    • \i executes a script.
    • \q exits the interactive shell.
  • SQL commands can be executed directly in the interactive shell, ended with ';'.

⚠️ **GitHub.com Fallback** ⚠️