Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Context:

I'm automatically installing postgresql-9.1 on an Ubuntu server with apt-get. This creates the required postgres user.

The Postgres data is on an external volume that survives reinstalls. This data is obviously owned by the postgres user.

The problem I'm having is that the ownership is not recorded under the name postgres, but under the UID that postgres had at creation time. When the server is reinstalled, postgres sometimes gets a different UID, and no longer owns the data directory, and thus does not work.


Question:

Can I force the UID of the user postgres created by apt-get to something fixed? Or is there another way to solve my problem?


(As you may have deduced, this is on Amazon EC2 with the data on an EBS volume)

share|improve this question
1  
Why not just pre-create the account? –  Zoredache Jun 19 '12 at 16:13
    
@Zoredache See Oliver's answer –  Bart van Heukelom Jun 19 '12 at 22:24
add comment

3 Answers

up vote 3 down vote accepted

Two ideas:

  1. Create your postgres user before installing PostgreSQL, using the correct UID.
  2. Create your own custom postgresql-9.1 package which installs the stock postgresql-9.1 package and then does the chown.
share|improve this answer
    
If I create the user (option 1) with no parameters (just the UID), would apt-get configure things like the home directory? –  Bart van Heukelom Jun 19 '12 at 15:11
1  
No, of course you would need to create the user with the correct settings. See /var/lib/dpkg/info/postgresql-common.postinst for how the user is created (that's on Debian, but I'm pretty sure it is the same or very similar on Ubuntu). –  Oliver Jun 19 '12 at 15:15
    
If the user exists, the script skips the adduser part, but does all the rest. –  Oliver Jun 19 '12 at 15:16
    
The command used is adduser --system --quiet --home /var/lib/postgresql --no-create-home --shell /bin/bash --group --gecos "PostgreSQL administrator" postgres. I could easily do that before installation. Thanks for pointing me to the postinst script, that'll be good to know for other situations too. –  Bart van Heukelom Jun 19 '12 at 15:34
add comment
usermod -u {desired_UID} {username}

Stole this from here: Lethe's blog

But I actually think that a chown would be better than this, since it cannot fail on account of the desired_UID being already used or conflict laden in other ways.

share|improve this answer
    
Could work, though then I would also have to fix the ownership of various other files that apt-get creates. –  Bart van Heukelom Jun 19 '12 at 15:05
add comment

I could add a chown of the data directory to the server setup script, but I'd prefer a more elegant solution. Also because there are other services, like Tomcat, which have the same problem.

share|improve this answer
add comment

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.