0

I am newbie with Rails and here is my problem.

I created a Rails by this command

rails new freelancer --database=postgreesql

And I had no error, everything seems ok. When I opened the file database.yml, here is what I got

default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> 
development:
<<: *default
database: freelancer_development
test:
<<: *default
database: freelancer_test
production:
<<: *default
database: freelancer_production
username: freelancer
password: <%= ENV['FREELANCER_DATABASE_PASSWORD'] %>

So, I thought that I must set the User Password for thist PostgreSQL database. I search and found this link PostgreSQL: How to change PostgreSQL user password?, follow it and used this command

sudo -u freelancer psql freelancer_production

But it said to me that

sudo : unknown user : freelancer
sudo : unable to initialize policy plugin

I am very confusing with this problem because I defined freelancer above ? Could you please give me some ideas ? Thank you very much.

Nguyen Cuc
  • 89
  • 1
  • 9

1 Answers1

1

First things first, this is an issue about accessing the database, not necessarily related to rails. What you're facing is that postgres does not have a user called 'freelancer', thus you cannot access the db.

There are two ways of solving this issue:

A. Create a database user for the 'freelance' user

Basically what you want to do is:

  • login as the root user and create a new user/role
$ sudo -u postgres psql
> CREATE ROLE rolename LOGIN SUPERUSER PASSWORD 'test_password';

There are many references on how to do this if you want to learn more

B. Use the root postgres user to access the database

In this case we just need to change database.yml file:

  • set the username as postgres
  • set the password the root password for your postgres database

All that said, please be aware that using a SUPERUSER role is not ideal and presents security risks. So never use it outside your localhost environment or in a database that holds important information.

brunopagno
  • 46
  • 4
  • 1
    The immediate problem is coming from sudo, not from PostgreSQL. The problem is that the *OS user* "freelance" doesn't exist. – jjanes Apr 01 '21 at 23:57
  • I absolutely missed that. Still the solution applies, because it is necessary to create the user on Postgres. Nice catch jjanes – brunopagno Apr 02 '21 at 20:34