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… :)

7 Responses to “Creating New Accounts in PostgreSQL”


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

  2. 2 Daz

    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.

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

  4. 4 Newbie

    $ 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 :)

  5. 5 Douglas

    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.

  6. 6 lautaro

    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.

  7. 7 Ashesh

    Great post! Was very helpful to me!

  1. 1 Setup PostgreSQL Database server in Ubuntu « Going GNU

Leave a Reply