Infrastructure at your Service

Do you need a hundred databases and users for training etc. in PostgreSQL?
Read More
Just a few lines of code:

[[email protected] data]$ cat create_databases.sh
 #!/bin/bash
 NUMOFDBS=100
 for i in `seq ${NUMOFDBS}`; do
 psql -q -c "create user u${i} UNENCRYPTED password 'u${i}' NOCREATEDB NOCREATEROLE NOCREATEUSER" postgres
 createdb -O u${i} db${i}

done

Execute it and you are ready:

[[email protected] data]$ time ./create_databases.sh
real 1m37.825s
 user 0m0.143s
 sys 0m0.109s

Have a look at the databases and its owners:

postgres=# \l+

Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
 -----------+-----------+----------+-------------+-------------+-------------------------+---------+------------+--------------------------------------------
 db1 | u1 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 6705 kB | pg_default |
 db10 | u10 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 6705 kB | pg_default |
 db100 | u100 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 6705 kB | pg_default |
 db11 | u11 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 6705 kB | pg_default |
 db12 | u12 | UTF8 | en_US.UTF-8 | en_US.UTF-8 | | 6705 kB | pg_default |
 ...

Really, really easy. For dropping the databases:

[[email protected] data]$ cat drop_databases.sh
 #!/bin/bash
NUMOFDBS=100
for i in `seq ${NUMOFDBS}`; do
 dropdb db${i}
 dropuser u${i}
done

One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Daniel Westermann
Daniel Westermann

Principal Consultant & Technology Leader Open Infrastructure