55

i'm looking for setup a Rails Environment with Vagrant, for that purpose the box it's been provisioned through bash shell method and includes among others this line:

sudo -u postgres createuser <superuserusername> -s with password '<superuserpassword>'

but i'm getting a configuration error, createuser: too many command-line arguments (first is "with")

can you help me with the correct syntax for create a Superuser with a password. Thanks

Joseph Palmezano
  • 1,245
  • 2
  • 11
  • 16

4 Answers4

75

Do it in a single statement within psql:

CREATE ROLE username WITH LOGIN SUPERUSER PASSWORD 'password';

e.g

CREATE ROLE dummy WITH LOGIN SUPERUSER PASSWORD '123456';

Eric
  • 22,183
  • 20
  • 145
  • 196
46

Solved with:

sudo -u postgres createuser -s -i -d -r -l -w <<username>>
sudo -u postgres psql -c "ALTER ROLE <<username>> WITH PASSWORD '<<password>>';"

I know is not an elegant solution, but for now it'll do

Joseph Palmezano
  • 1,245
  • 2
  • 11
  • 16
33

For PostgreSQL versions 8.1 and newer

To create a superuser:

CREATE USER username SUPERUSER;

If you need to specify the password:

CREATE USER username WITH SUPERUSER PASSWORD 'passwordstring';
Yegor
  • 3,652
  • 4
  • 22
  • 44
Kaka Ruto
  • 4,581
  • 1
  • 31
  • 39
16

To create a PostgreSQL user, follow these steps: At the command line, type the following command as the server's root user:

su - postgres

You can now run commands as the PostgreSQL superuser.To create a user, type the following command:

createuser --interactive --pwprompt

At the Enter name of role to add: prompt, type the user's name.
At the Enter password for new role: prompt, type a password for the user.
At the Enter it again: prompt, retype the password.
At the Shall the new role be a superuser? prompt, type y if you want to grant superuser access. Otherwise, type n.
At the Shall the new role be allowed to create databases? prompt, type y if you want to allow the user to create new databases. Otherwise, type n.
At the Shall the new role be allowed to create more new roles? prompt, type y if you want to allow the user to create new users. Otherwise, type n.

PostgreSQL creates the user with the settings you specified.

Rakesh
  • 793
  • 4
  • 22
  • 1
    The matter is i'm looking to create a superuser with password, through a script which executes sequentially. `sudo -u postgres createuser postgres with password 'postgres' -s # Set super unsafe defaultz (dev only) sudo sh -c "echo 'local all postgres peer\nlocal all all peer\nhost all all 127.0.0.1/32 md5\nhost all all ::1/128 md5' > /etc/postgresql/10/machine/pg_hba.conf" sudo /etc/init.d/postgresql reload` I'm a noob so, can you clarificate your answer? am i in the correct via? – Joseph Palmezano Sep 17 '19 at 14:40