download and pipe sql to psql

7,704

Solution 1

If both command are working, you should try

curl -s http://s3.aws.com/backup.sql | sudo -u postgres psql

Solution 2

I know this is an old question, but I just stumbled upon during a search (it was the top of the results) and it does not look like any of the answers are correct according to the postgres manual.

The manual suggests passing the -f - as an option to psql and then piping over stdin what ever you want. Quoting the manual for 9.4:

If filename is - (hyphen), then standard input is read until an EOF indication or \q meta-command. Note however that Readline is not used in this case (much as if -n had been specified).

Using this option is subtly different from writing psql < filename. In general, both will do what you expect, but using -f enables some nice features such as error messages with line numbers.

Solution 3

Yet another way:

sudo -u postgres psql --file <(curl -s http://s3.aws.com/backup.sql)
Share:
7,704

Related videos on Youtube

american-ninja-warrior
Author by

american-ninja-warrior

Updated on September 18, 2022

Comments

  • american-ninja-warrior
    american-ninja-warrior over 1 year

    How can I do this in one step?

    Download backup command:

    curl http://s3.aws.com/backup.sql
    

    Restore backup command:

    sudo -u postgres psql <PIPE-THE-OUTPUT-OF-PREVIOUS-COMMAND>