Postgresql error after installation - ruby-on-rails

I already have a rails project then i installed Postgresql and pgadmin III , I need to use posgresql in development instead of sqlite and production to use heroku , I followed the answer number two in this question Change from SQLite to PostgreSQL in a fresh Rails project because i just installed Postgresql using synaptic center without making anything else , when i try to create a user i got an error:
createuser dexter
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) y
Shall the new role be allowed to create more new roles? (y/n) y
createuser: could not connect to database postgres: FATAL: role "dexter" does not exist

use:
$ sudo -u postgres createuser
To answer your question, from the postgresql documentation:
"If you wish to create a new superuser, you must connect as a
superuser, not merely with CREATEROLE privilege. Being a superuser
implies the ability to bypass all access permission checks within the
database, so superuserdom should not be granted lightly."
Given that the account you are creating is simply for granting your Rails application access, while the ability to create databases makes sense, superuser privileges is not a good idea.

Related

Rails: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations

I'm trying to create the database in Rails. In Postgres I see the development and test database, however, I'm getting a permissions error. I've tried to follow this link, didn't work for me.
Error: PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations : SELECT "schema_migrations".* FROM "schema_migrations"
Rails: permission denied for relation schema_migrations
default: &default
adapter: postgresql
encoding: unicode
pool: 5
host: localhost
username: root
password:
development:
<<: *default
database: svp-chicago_development
I log into postgres and did these commands.
psql postgres
CREATE USER root
CREATE DATABASE svp-chicago_development
GRANT ALL PRIVILEGES ON DATABASE svp-chicago_development to root
ALTER DATABASE svp-chicago_development OWNER TO root
When I do \list I see the database is there.
I had same issue and I solved by adding "Superuser" to the role.
First, list users and their privileges. If you followed above commands, root user does not have "Superuser" Attributes.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
other | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
root | | {}
Next, upgrade root to be a "Superuser".
postgres=# ALTER USER root WITH SUPERUSER;
ALTER ROLE
Again, list users and their privileges. Now root has "Superuser".
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
other | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
root | Superuser | {}
Hope it helps.
I guess you missed create password for your user. Try to create password as following:
CREATE USER root WITH PASSWORD 'your_new_password';
CREATE DATABASE svp-chicago_development;
GRANT ALL PRIVILEGES ON DATABASE svp-chicago_development to root;
ALTER DATABASE svp-chicago_development OWNER TO root;
I had this issue when working on a Rails 6 application with PostgreSQL.
The first check is to ensure that you've granted all privileges for a database to the particular user that you want using:
GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myusername;
But in my own case, the cause of the issue was that I created a Database and then granted all privileges to a particular user. After some time, I granted another user the privileges using GRANT ALL PRIVILEGES ON DATABASE mydatabase TO myusername;
So the second user even though I granted all privileges to it, didn't have permissions to perform actions on the database tables.
Here's how I fixed it:
Log into the PostgreSQL console where the database is stored:
sudo -u postgres psql
List all databases in that PostgreSQL database server:
\l
OR
\list
Connect to the database that you want to fix it's permissions:
\c mydatabase
OR
\connect mydatabase
List all tables in the current database using your search_path:
\dt
OR
List all tables in the current database regardless of your search_path:
\dt *.
You will notice that the tables still reference the initial user or role as the owner.
Now you will have to modify the tables to reference the new user or role as the owner.
You can modify each table individually using this:
ALTER TABLE table_name OWNER TO new_owner;
This does not require specifing the old_owner. It is essential when the user is postgres (the default database user) and you want to modify the owner to a new user.
OR modify all the tables simultaneously using this:
REASSIGN OWNED BY old_owner TO new_owner;
This requires specifing the old_owner. It is essential when you have already modified the user from postgres (the default database user) to another user and you want to modify the owner to a new user.
Note: Ensure that you are connected to the database that you want to modify privileges/permissions for, else you might run into errors.
Also, if you're using a service like Heroku, it's worth checking to see if you have overrun your row limit and write access has been revoked in the database.
You can do that by going to the dashboard, click on the app, click on the postgres service icon, then check the row limit.
Just in case someone else comes here with the same issue, I did try many other solutions and the one that worked for me the best was the following: Modify OWNER on all tables simultaneously in PostgreSQL
This worked since my user (e.g. root or postgres) had Superuser privileges so trying REASSIGN OWNED gives error when trying to assign system objects
ALTER DATABASE didn't work since the issue is on a table object ownership and not in the DB ownership. Altering the owner on the DB doesn't propagate to the other object on that DB schema
Try listing your tables to see who the owner is. In my case, I had imported data via psql < dump.sql and all the imported tables were owned by postgres instead of my user.
To check this, start psql and enter the command \dt within your database. Look for the following line:
public | schema_migrations | table | postgres
Not good! It's owned by postgres and you'll need to fix that:
You can use #ddreliv's solution here to reassign the owner of every table, or
if you have a dump, simply drop the database and re-import it with the proper user. For example, use sudo -u my_user psql < dump.sql instead of sudo -u postgres psql < dump.sql.
Here's how I fixed it:
Log into the PostgreSQL console where the database is stored:
sudo -u postgres psql
List all databases in that PostgreSQL database server:
\l
OR
\list
Connect to the database that you want to fix it's permissions:
\c mydatabase
OR
\connect mydatabase
List all tables in the current database using your search_path:
\dt
OR
List all tables in the current database regardless of your search_path:
\dt *.
Then giving privilleages to the table :-
One Table
GRANT ALL PRIVILEGES ON TABLE side_adzone TO jerry;
All Tables of schema
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO jerry;
for our case (with docker-compose)
remove postgres data
$ docker-compose down # CAUTION: may cause data loss
$ docker volume rm XXX_DB_VOLUME # CAUTION: will cause data loss
then downgrade postgres from 11.11 to 11.8 in docker-compose.yml
xxx_db:
image: "postgres:11.8"
finally start postgres docker again
$ docker-compose up -d
Problem solved.
Adding "Superuser" to the role is a security issue, superusers can drop databases not only the svp-chicago_development pointed out by the OP but any other database existent.
I believe the issue that OP have is related to tables owner as pointed out by #kevin-cooper.
Even if the OP did the commands that he said granting all privileges and owner to the database, if the OP still runs any table creation or restore dump with the postgres credential the tables on the database svp-chicago_development it will have postgres as owner and if the application in running with other user/role it will throw the error mentioned by the OP.
You can check if that's the case in psql by:
\c svp-chicago_development
then listing the tables:
\dt
It will show something like:
Schema | Name | Type | Owner
--------+------------+-------+----------
public | table_name | table | postgres
If you have a few rows you can manually change the owner of each row by running:
ALTER TABLE table_name OWNER TO new_owner;
If you are running a pg_restore make sure to pass --role so objects will be owned by the role specified.

myRailsApp - PostgreSQL FATAL error

I'm very new with ruby and coding in general, I'm working on a ruby on rails project and PostgreSQL is giving me an error: FATAL:role "myRailsApp" does not exist. googled for posible solutions and couldn't find anything. thanks
You don't have a role with that name created in the Postgres DBMS. If you are new, you probably should download Pgadmin (http://www.pgadmin.org/), there you can configure your postgres database.
When you open it you will see your servers and stuff, loof for the 'Login roles' part and create a new role that can create databases (That role needs to have the same user and password you chose on the configuration file)
https://www.youtube.com/watch?v=1wvDVBjNDys

How to solve psql: FATAL: role "ubuntu" does not exist

When i run rails db i get the below error.
psql: FATAL: role "ubuntu" does not exist
First i want to know the exact meaning of what it says before solving it.
I have tried http://stackoverflow.com/questions/11919391/postgresql-error-fatal-role-username-does-not-exist this link as well but nothing seems to workout for me ..
I don't have a Rails-specific answer for you, but I can tell you some possibly helpful facts about PostgreSQL authentication:
PostgreSQL uses the term "role" to mean "user" or "login", so the error message means you are trying to connect to the PostgreSQL database with a username of ubuntu, but no such user exists.
If you don't explicitly tell PostgreSQL what user you want to log in as, it will use your operating system credentials. That is, if you're logged in to Linux as the user ubuntu, PostgreSQL will try to log you in as a PostgreSQL user named ubuntu.
PostgreSQL authentication is configured in a file called pg_hba.conf. See http://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html. However, if you're using Rails, I suspect there's a way for you to control the PostgreSQL authentication setup without editing the pg_hba.conf file manually.

creating user for postgresql on rails

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?
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
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
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.
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)?
You become postgre and make the shell a login shell
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.
You quit the postgres shell session.
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.
You become postgres and make the shell a login shell
You log into postgres command shell by default with user postgres
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.

Rails and PostgreSQL: Role postgres does not exist

I have installed PostgreSQL on my Mac OS Lion, and am working on a rails app. I use RVM to keep everything separate from my other Rails apps.
For some reason when I try to migrate the db for the first time rake cannot find the postgres user. I get the error
FATAL: role "postgres" does not exist
I have pgAdmin so I can clearly see there is a postgres user in the DB - the admin account in fact - so I'm not sure what else to do.
I read somewhere about people having issues with PostgreSQL because of which path it was installed in, but then I don't think I would have gotten that far if it couldn't find the db.
Actually, for some unknown reason, I found the issue was actually because the postgresql role hadn't been created.
Try running:
createuser -s -r postgres
Note that roles are the way that PostgreSQL maintains database permissions. If there is no role for the postgres user, then it can't access anything. The createuser command is a thin wrapper around the commands CREATE USER, CREATE ROLE, etc.
Recently i got this problem immediately after installing postgres.
If it comes immediately after installation, you might be missing the default user, postgres.
In that case, you can create default user postgres using below command.
createuser -s -U $USER
Ex: createuser -s -U $USER
enter your required role name: postgres
enter password for your the user:
It will prompt you to enter required database role name and password
Once you complete the process, you can login to the postgres console using below command
psql -U 'your_database_name'
Ex: psql -U postgres
Here, You need to enter the password if you have given any, while creating the user.
Hope it helps :)
I was on OSX 10.8, and everything I tried would give me the FATAL: role "USER" does not exist. Like many people said here, run createuser -s USER, but that gave me the same error. This finally worked for me:
$ sudo su
# su postgres
# createuser -s --username=postgres MYUSERNAME
The createuser -s --username=postgres creates a superuser (-s flag) by connecting as postgres (--username=postgres flag).
I see that your question has been answered, but I want to add this answer in for people using OSX trying to install PostgreSQL 9.2.4.
This message pops up, when the database user does not exist. Compare the manual here.
Multiple local databases cannot be the explanation. Roles are valid cluster-wide. The manual again:
Note that roles are defined at the database cluster level, and so are
valid in all databases in the cluster.
You must be ending up in another database-cluster. That would be another server running on the same machine, listening to a different port. Or, more likely, on a different machine.
Could it be that the message comes, in fact, from the remote server?
I met this issue right on when I first install the Heroku's POSTGRES.app thing. After one morning trial and error i think this one line of code solved problem. As describe earlier, this is because postgresql does not have default role the first time it is set up. And we need to set that.
sovanlandy=# CREATE ROLE postgres LOGIN;
You must log in to your respective psql console to use this psql command.
Also noted that, if you already created the role 'postgre' but still get permission errors, you need to alter with command:
sovanlandy=# ALTER ROLE postgres LOGIN;
Hope it helps!
In the Heroku documentation; Getting started whit rails 4, they say:
You will also need to remove the username field in your database.yml
if there is one so: In file config/database.yml remove: username:
myapp
Then you just delete that line in "development:", if you don't pg tells to the database that works under role "myapp"
This line tells rails that the database myapp_development should be
run under a role of myapp. Since you likely don’t have this role in
your database we will remove it. With the line remove Rails will try
to access the database as user who is currently logged into the
computer.
Also remember to create the database for development:
$createdb myapp_development
Repleace "myapp" for your app name
The installation procedure creates a user account called postgres that is associated with the default Postgres role. In order to use Postgres, you can log into that account. But if not explicitly specified the rails app looks for a different role, more particularly the role having your unix username which might not be created in the postgres roles.
To overcome that, you can create a new role, first by switching over to the default role postgres which was created during installation
sudo -i -u postgres
After you are logged in to the postgres account, you can create a new user by the command:
createuser --interactive
This will prompt you with some choices and, based on your responses, execute the correct Postgres commands to create a user.
Pass over a role name and some permissions and the role is created, you can then migrate your db
Could you have multiple local databases? Check your database.yml and make sure you are hitting the pg db that you want. Use rails console to confirm.
My answer was much more simple. Just went to the db folder and deleted the id column, which I had tried to forcefully create, but which is actually created automagically. I also deleted the USERNAME in the database.yml file (under the config folder).
In Ubuntu local user command prompt, but not root user, type
sudo -u postgres createuser username
username above should match the name indicated in the message "FATAL: role 'username' does not exist."
Enter password for username.
Then re-enter the command that generated role does not exist message.
I ended up here after attempting to follow Ryan Bate's tutorial on deploying to AWS EC2 with rubber. Here is what happened for me:
We created a new app using "
rails new blog -d postgresql
Obviosuly this creates a new app with pg as the database, but the database was not made yet. With sqlite, you just run rake db:migrate, however with pg you need to create the pg database first. Ryan did not do this step. The command is rake db:create:all, then we can run rake db:migrate
The second part is changing the database.yml file. The default for the username when the file is generated is 'appname'. However, chances are your role for postgresql admin is something different (at least it was for me). I changed it to my name (see above advice about creating a role name) and I was good to go.
Hope this helps.
After a bunch of installing and uninstalling of Postgres, here's what now seems to work consistently for me with Os X Mavericks, Rails 4 and Ruby 2.
In the database.yml file, I change the default usernames to my computer's username which for me is just "admin".
In the command line I run rake db:create:all
Then I run rake db:migrate
When I run the rails server and check the local host it says "Welcome aboard".
You might be able to workaround this by running initdb -U postgres -D /path/to/data or running it as user postgres, since it defaults to the current user. GL!

Resources