1

I need to run some commands which are not part of SQL, eg: \d (describe)

One normally runs such commands by first connecting to the database using the psql client, in the command line.

But now I need to do it programatically, using a script. How can I do this? I know about exec(), but psql isn't a normal command. It opens a client in the console, and THEN you run your commands. Kind of like ssh.

1
  • 1
    psql is a "normal command". It doesn't need a terminal on it's stdin/stdout, and psql databasename <inputfile >outputfile works as expected. (but you will need to quote the backslashes at some point(s) ) Commented Dec 11, 2015 at 14:40

1 Answer 1

1

The -c option solved it.

psql [...] -o output.txt -c "\d mytable"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.