Creating New Accounts in PostgreSQL

Getting a new account set up on PostgreSQL is a simple process…

Create our new user:

$ sudo su postgres -c createuser daz

Then you have to give this new user role a name (I called it daz), and then say ‘y’ to the question “Shall the new role be a superuser?” if you want the user to be an administrator.

Give the user a database password (this does not have to be the same as their unix password):

$ sudo su postgres -c psql
postgres=# ALTER USER daz WITH PASSWORD ‘mypassword’;
postgres=# \q

Finally, give the new user a database to play with:

$ sudo su postgres -c createdb daz

Pretty straight forward… :)

Spread the Word
  • Twitter
  • del.icio.us
  • Digg
  • Facebook
  • Google Bookmarks
  • Reddit
  • NewsVine
  • Slashdot
  • connotea
  • HackerNews
  • Print
  • email

8 Responses to “Creating New Accounts in PostgreSQL”


  • Newkidontheblock

    Hi Darren,

    sudo su postgres -c createdb daz

    This did not work for me….I’ve been having this problem since and beginning to think postgresql is a pain }:)

    it came up with this error

    createdb: database creation failed: ERROR: database “postgres” already exists

    can you help ..I need to get this right..cheers

  • Hi ya,

    Right, need to check a couple of things first…

    * You are running this command as your user aren’t you, not the ‘postgres’ user?
    * Your user account does have root priveleges?
    * Are you running this command: $ sudo su postgres -c createdb postgres?

    If it is the last one (as the error message you are getting suggests this), you are trying to create a database that already exists - you don’t need to create a database for the postgres user as there is one created by default upon install. You only need to create a database for other users that you create.

    Let me know if you’re still having troubles.

  • Newkidontheblock

    Hi,
    No…I did $ sudo su postgres -c createdb xyz … and got the error

    createdb: database creation failed: ERROR: database “postgres” already exists

    then I changed to the su user I already created … and I did

    $ sudo su newkid -c createdb xyz

    and I still got the same error:

    ERROR: database “postgres” already exists.

    I however Installed phppgadmin and logged in as the newkid user then created the database xyz from there and it worked.

    i don’t know why it won’t do it from the command line.

    Cheers

  • $ sudo su postgres -c createdb xyz … and got the error

    createdb: database creation failed: ERROR: database “postgres” already exists

    Gave me exactly the same error message so I separated the steps :

    user01~$ sudo su postgres
    postgres~$ createdb xyz
    CREATE DATABASE
    postgres~$ exit
    user01~$

    Worked for me :)

  • Strangely, was having the same problem and also the problem that it would not take my password when I tried no sign on.
    Newbies answer worked for me and made the database and now I can sign on.

  • Hi Daz, you can also do:

    $ sudo su postgres -c psql
    postgres=# CREATE USER daz WITH PASSWORD ‘mypassword’;
    postgres=# \q

    Instead “ALTER”, supposing “sudo su postgres -c createuser daz” wasn’t executed.

    Thanks, and nice blog.

  • Great post! Was very helpful to me!

  • I know this is a bit late, but…
    The “ERROR: database “postgres” already exists” problem is caused by missing quotes.
    su wants the command and its arguments to be passed in as one argument.
    So
    sudo su postgres -c “createdb blah”
    will create a database called blah.
    sudo su postgres -c createdb blah (no quotes)
    seems to be using the default db - which is the same as the username.

Leave a Reply