Is there a way to configure PostgreSQL to create a database whose name is the same as the username on the user's first login?


In pg_hba.conf there will be a rule something like:

local sameuser all ident sameuser

When a user connects to PostgreSQL via console/psql,

  1. If a database with the users name exists, just connect to database.
  2. If it does not exist, create a database named after the user and connect the user to the newly created database.

There are a lot of users and I do not want to manually create these databases all at once, upfront.

  • What's your plan with those many users? Are they users of an application? Sep 27, 2012 at 20:32
  • Users are students who want to learn about databases.
    – J_R_
    Sep 27, 2012 at 20:39
  • 3
    I suggest then creating a shell script. You have to test for existence of user's DB, then create DB an user if necessary, and only then log in user. Sep 27, 2012 at 20:53

2 Answers 2


No that's not possible.

Even if it was possible: as you need to specify a database when you connect, the new database wouldn't be the one the user connected to (and there is no way to re-route the user to the newly created one).


In the LedgerSMB project we created a simple web app to create the database specified. Superuser logs in, it detects whether the db exists, and offers to create it if not. You could do the same thing here and have your help desk create the db's. Of course if you aren't doing the things we are doing you won't need to be superuser, just have the right to create databases (and, also roles, see below), so you may want the app to access the db as a superuser anyway but depending on your design you may not need to give out superuser credentials.

The other issue I can see is that the database isn't the only thing you will have to create. ident sameuser/peer authentication works fine, but it requires that the user account exists in both the system and the database. So your app will need to create the role as well. For those with ident access that means they are logging into the shell first, and then into the db, right? So I assume that a shell script might be sufficient as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.