Skip to content

Quickly create a hundred databases and users

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

[postgres8@oel7 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:

[postgres8@oel7 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:

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

Let’s work together

Ready to operate at
the next level?

Tell us about your challenge. We’ll bring the depth, the expertise, and the Swiss precision.

Open source Team

Open source Team

Posts published under the Open source Team account are collective contributions by the dbi services open infrastructure consultants. The practice covers open-source databases and platforms, PostgreSQL first of all, along with MySQL, MariaDB, and Linux, from architecture, replication, and high availability to performance tuning and operations under service level agreements, and contributes actively to the PostgreSQL community.View Author posts

721 posts since September 2011

Leave a Comment

Your email address will not be published. Required fields are marked *