I am trying to write a bash script that creates and sets up a postgres database if it does not already exist. Following this, I've got to this script:
if [ `psql -l | grep mydb | wc -l` -eq 1]
then
echo "Database exists"
else:
echo "Creating database"
...
fi
But it always returns with "Database exists"
, regardless of whether the database is there or not. I've also tried [ "` psql -l | grep mydb | wc`" == "1" ]
, but it gives the same result. What am I doing wrong?
EDIT The output of psql -l
and psql -l | grep mydb
are:
$ psql -l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+--------------+----------+-------------+-------------+-----------------------
geoserver | adminhgv7rj4 | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
project_db | gis_group | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(6 rows)
$ psql -l | grep mydb
$