0

I use these commands:

su - postgres // change user to 'postgres'
dropdb db1    // drop database 'db1'
exit          // change user back to root

These commands work well from console, so I created a small sh file with the same lines and added a -x chmod to execute the file. However, when I execute the Script, only the first line and changes the user to postgres and stops there with the blinking cursor. Then, if I type exit to return back to root, it shows the message: dropdb: error: could not connect to database template1: FATAL: role "root" does not exist.

How can I get this working?

2
  • 1
    Try PGUSER=postgres dropdb db1. Commented Jan 5, 2022 at 15:14
  • …or simply dropdb -u postgres db1 Commented Jan 5, 2022 at 15:24

1 Answer 1

1
#!/bin/bash
sudo -u postgres psql -c 'drop database IF EXISTS test'
exit
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.