4

I chose postgresql for my database for rails but i ran into an apparently common error where 'FATAL: role "app" does not exist' when i try to run rake db:create:all. I found two solutions but im not sure which one is the right one to use. One website says to

su -  
su - postgres
createuser -s Application
exit
exit  

while the other says to

su - postgres
create role myapp with createdb login password 'password1'

what's the difference between the two? and which one should i use?

1
  • 1
    The second is invalid - you're mixing a shell command and a sql command. I would do the first, though I think it only works on debian systems. And I wouldn't give it superuser capabilities. Commented Apr 17, 2014 at 6:16

3 Answers 3

19

You should use this for the development environment only

Login in postgres console:

$> sudo -u postgres psql

create user with name rails and password:

=# create user rails with password 'password';  

make user rails superuser:

=# alter role rails superuser createrole createdb replication; 

create database projectname with owner rails:

=# create database projectname owner rails;

in database.yml:

development:
  adapter: postgresql
  encoding: unicode
  database: projectname
  pool:
  username: rails
  password: password
Sign up to request clarification or add additional context in comments.

2 Comments

may I ask how about production env?
Thank you for this answer! The only part that I didn't need was this one: =# create database projectname owner rails;
-1

To create a user type this in your console

createuser

Enter the username and make it a superuser

Also you could add a password also by adding pwprompt to createuser like this

createuser --pwprompt

Next you enter pgsql as postgres and create your database

sudo -u postgres psql postgres create database db_name owner user_name;

Hope it helps

Refer this site for more details https://www.digitalocean.com/community/articles/how-to-install-and-use-postgresql-on-ubuntu-12-04

Comments

-2

The second option is invalid. That does not work.


The first one:

I don't recommend the first one and depending on your operating system some of the steps are unnecessary.

  1. You become a superuser and make the shell a login shell (as if you would log in directly). I think it is risky and not necessary. Why not suing directly to postgre user (that's just a matter of setup)?
  2. You become postgre and make the shell a login shell
  3. You create a superuser Application in PostgreSQL. So you have the privileges of a superuser for your application. I think that is risky too. Superuser for an application is very bad. Often you need createdb, login and password to get started and other privileges, if ever needed, can be added/revoked later.
  4. You quit the postgres shell session.
  5. You quit the superuser shell session

The second one:

That's not correct. You forget to execute psql command as second step. The second command would become the third. It would be like that.

  1. You become postgres and make the shell a login shell
  2. You log into postgres command shell by default with user postgres
  3. You create a role myapp with options createdb, login and (usually by default encrypted) password with value password1.

The difference is that the first one grants superuser privileges to the Application role while the second grants only the listed permissions to the myapp role.
The second option is more secure. Among the two option opt for the seoconde one.


Note: In PostgreSQL a user is simply a role with login permission. It's more of a conceptual distinction. Technically a user and role are pretty much the same as far as I know.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.