Home > Enterprise >  Shell script to drop PostgreSQL database
Shell script to drop PostgreSQL database

Time:01-07

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?

CodePudding user response:

#!/bin/bash
sudo -u postgres psql -c 'drop database IF EXISTS test'
exit
  •  Tags:  
  • Related