Database Reference
In-Depth Information
How to do it...
The easiest command is the one that executes a single SQL command and prints the output as:
$ psql -c "SELECT current_time"
timetz
-----------------
18:48:32.484+01
(1 row)
The -c command is non-interactive. If we want to execute multiple commands, we can write
those commands in a text file, and then execute them using the -f option. The following
command loads a very small and simple set of examples:
$ psql -f examples.sql
which produces the following output when successful:
SET
SET
SET
SET
SET
SET
CREATE SCHEMA
SET
SET
SET
DROP TABLE
CREATE TABLE
DROP TABLE
CREATE TABLE
The script examples.sql is very similar to a dump file produced by PostgreSQL backup
tools, so this type of file, and the output it produces, are very common. PostgreSQL produces
the name of the command as a "command tag" when it executes successfully, which is what
produces the preceding output.
psql can also be used in interactive mode, which is the default, so requires no option:
$ psql
postgres=#
The first interactive command you'll need is the following:
postgres=# help
You can then type in SQL or other commands.
 
Search WWH ::




Custom Search