Chapter 3. psql

psql is the de rigueur command-line utility packaged with PostgreSQL. Aside from its most common use of running queries, you can use psql as an automated scripting tool, as a tool for importing or exporting data, restoring, database administration, and even go so far as to use it as a minimalistic reporting tool. psql is easy to use. Like any other command-line tool, you just have to be familiar with the myriad of switches involved. If you only have access to a server’s command line with no GUI, psql is pretty much your only choice for querying and managing PostgreSQL. If you fall into this category, we suggest that you print out the dump of psql help from the psql: Interactive and Scriptable and frame it right above your workstation.

Just as the other command-line tools packaged with PostgreSQL, you can forgo explicitly specifying, host, port, user by setting the environment variables PGHOST, PGPORT, PGUSER as described in Environment Variables and setting PGPASSWORD or using a password file as described in The Password File. Should you omit the parameters without having set the environment variables, psql will use the standard defaults. For examples in this chapter, we’ll assume you are using default values or have these variables set. If you’re using pgAdmin as well, you can jump right to psql using the plugin interface, (see Accessing psql from pgAdmin). A console window will open with psql and already connected directly to the database in pgAdmin.

Interactive ...

Get PostgreSQL: Up and Running now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.