Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

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?

Scenario:

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.

share|improve this question
    
What's your plan with those many users? Are they users of an application? –  dezso Sep 27 '12 at 20:32
    
Users are students who want to learn about databases. –  J_R_ Sep 27 '12 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. –  dezso Sep 27 '12 at 20:53

2 Answers 2

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.

share|improve this answer

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).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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