Setup:
- Local *nix machine with a SQL script
script.sql(Postgres). - Remote machine
remotewith Postgres.- I can SSH in as
some_user, who is a sudoer. - Anything with Postgres needs to be done as
postgresuser. - The server only listens on
localhost:5432.
- I can SSH in as
How do I execute script.sql on remote without copying it there first?
This works well:
ssh -t some_user@remote 'sudo -u postgres psql -c "COMMANDS FOO BAR"'
The -t flag means that sudo will ask for some_user's password correctly on the local terminal.
One thing remains, to be able to pipe script.sql to psql. This does not work:
ssh -t some_user@remote 'sudo -u postgres psql' < script.sql
It fails with the message:
Pseudo-terminal will not be allocated because stdin is not a terminal.
sudo: no tty present and no askpass program specified
