I'm trying to write a quick line in bash that will use psql to connect to a database and delete all entries in two tables:

\connect some_db
DELETE from some_table
DELETE from another_table

I'd like to do this in one line of bash scripting like so:

psql -U <username> -c "\c some_db; DELETE from some_table; DELETE from another_table"

but it seems like I can't do that because the \c command creates a new context that the subsequent commands don't apply towards.

What's the right way to do this in a line or two from the CLI?


1 Answer 1


You don't need the \c in the command specified with -c, you can specify the database as a parameter to psql:

psql -U <username> -d some_db -c "DELETE from some_table; DELETE from another_table"

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.