I'm looking for one-liner command for import & export SQL without login into postgres role or psql prompt.

Below is what I got for import SQL, but it asking password every time:

psql -h localhost -d dbname -U postgres -f /var/www/db.sql

Below is equivalent command in MySQL (import & export):

mysql -u root -p password dbname < /var/www/db.sql

mysqldump -u root -p password --single-transaction --skip-extended-insert --quick dbname > /var/www/db.sql

1 Answer 1


psql doesn't provide a command line option for the password to avoid disclosing it to other local users.

However it accepts the PGPASSWORD environment variable, so your equivalent of mysql's -p password is, in shell syntax:

PGPASSWORD=password psql -h localhost -d dbname -U postgres -f /var/www/db.sql

Alternatively, a client-side password file may be used.

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.