ClareSudbery
ClareSudbery

Reputation: 391

Difference between psql command and psql interactive terminal

I've tried various combinations of search terms and I can't find an answer to this...

I'm new to Postgres, and I'm enjoying using the psql interactive terminal to run SQL commands. But often when I look things up, I find people using psql as a command rather than as a terminal.

For instance, you can restore a database using this command:

psql database-name < path/to/backup.dmp

My question is, are they the same thing or different things? When I run psql as a standalone command, am I effectively running up an interactive terminal for just that one command? And if so, does that mean that anything which goes after psql will also work as a command typed into the psql terminal? So in the example above, I could also just start up a psql terminal and then run the following command?

postgres=# database-name < path/to/backup.dmp

Upvotes: 0

Views: 667

Answers (1)

Laurenz Albe
Laurenz Albe

Reputation: 246383

This is actually basic bash stuff. You should read up on Unix shells to understand that better.

Each process has a standard input, a standard output and a standard error. By default, the interactive terminal where you started a program will be used for these, so the text you type will be the input of the program, and the output of the program will be shown on your screen.

Bot you can also redirect standard input with

command < file

Then the input for the program will be taken from file rather than from the interactive terminal.

That's one of the ideas in Unix: the user is just another file from which you can read and to which you can write.

So everything before the < is part of the command invocation, and everything after the < is the file to read.

If you want to read and execute an SQL script while in a psql interactive session, use

\i file.sql

Upvotes: 1

Related Questions