commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
This command will "su" the execution of the command to the postgres user(implies that you are already logger as root), and export the result of the query to a file on the csv format.
You'll need to adequate the fields and database information to one of your choice/need.
Normally, if you su to another user from root and try to resume that other user's screen session, you will get an error like "Cannot open your terminal '/dev/pts/0' - please check." This is because the other user doesn't have permission for root's pty. You can get around this by running a "script" session as the new user, before trying to resume the screen session. Note you will have to execute each of the three commands separately, not all on the same line as shown here.
Credit: I found this at http://www.hjackson.org/blog/archives/2008/11/29/cannot-open-your-terminal-dev-pts-please-check.
Run program as root by SSH key forwarding instead of sudoers.
Put this alias line in .bashrc or wherever you like. Alias arguments might need extra escaping.
You might wonder about security. But you'd block out root login as much as possible of course. In sshd_config you put this:
Match Address 127.0.0.1
I've used this a number of times troubleshooting user permissions. Instead of just 'su - user' you can throw another hyphen and stay in the original directory.