0
votes

I have looked at similar questions and tried the methods such as editing the pg_hba file to trust, and doing ALTER USER postgres PASSWORD 'newPassword'; but neither worked. I did restart postgres after making such changes.

The context is I have a web application for a company I work for, and I am trying to run create data through the command bundle exec rake db:...

If it helps, I was initially getting the error "PG::ConnectionBad: could not connect to server: Connection refused Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432? "centos""

Note: I was on centOS when this occurred. tried switching to ubuntu, same deal. I then try installing PostgreSQL, as up until then, the bundle install command only installed 'pg'. This then changed the error message to the one this post is about. I am running Ubuntu 19.10 on VirtualBox. If anyone has any idea how to fix this, the help would be greatly appreciated; I've been trying to troubleshoot this for hours. For reference, the extended error message reads:

bundle exec rake db:create_organization['Organization Name']
rake aborted!
PG::ConnectionBad: FATAL:  password authentication failed for user "postgres"
FATAL:  password authentication failed for user "postgres"
/var/lib/gems/2.5.0/gems/pg-0.21.0/lib/pg.rb:56:in `initialize'
/var/lib/gems/2.5.0/gems/pg-0.21.0/lib/pg.rb:56:in `new'
/var/lib/gems/2.5.0/gems/pg-0.21.0/lib/pg.rb:56:in `connect'
/var/lib/gems/2.5.0/gems/activerecord-5.1.7/lib/active_record/connection_adapters/postgresql_adapter.rb:705:in `connect'
...
...
(more of the same)
...
...
/home/reeda19/dashboard/lib/tasks/onboarding/01_create_organization.rake:8:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.5.0/gems/rake-12.3.3/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:create_organization
(See full trace by running task with --trace)

Thank you again for your help

Edit: config/database.yml file:

default: &default adapter: postgresql encoding: utf8 host: localhost username: postgres password: password port: 5432 reconnect: true pool: 10

development: <<: *default database: dashboard_development

test: <<: *default database: dashboard_test

staging: <<: *default database: dashboard_staging username: dashboard_staging host: prod-diversitydashboard-db-01.c3zmqrgsly7w.us-east-2.rds.amazonaws.com password: <%= ENV['dashboard_DATABASE_PASSWORD'] %>

production: <<: *default database: dashboard_production username: dashboard_production host: prod-diversitydashboard-db-01.c3zmqrgsly7w.us-east-2.rds.amazonaws.com password: <%= ENV['dashboard_DATABASE_PASSWORD'] %>

Edit 2: Updated the postgres user password to match the one set in the database.yml file with ALTER USER user_name WITH PASSWORD 'new_password';. I no longer get a fatal password error, but am instead getting this new error:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "businesses" does not exist
LINE 8:                WHERE a.attrelid = '"businesses"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
                     c.collname, col_description(a.attrelid, a.attnum) AS comment
                FROM pg_attribute a
                LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                LEFT JOIN pg_type t ON a.atttypid = t.oid
                LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
               WHERE a.attrelid = '"businesses"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

Not sure 100% if this is a problem with my database specifically, or more with psql.

Will update upon any further progress.

FINAL EDIT: A simple rake db:reset fixed that new error! Seems like everything is in order now

1
Did you try that? stackoverflow.com/questions/7695962/… P.S Sorry, Don't have reputation to post a comment.GS Shahid
Yup I tried exactly that, no use :(Alex Reed

1 Answers

1
votes

Try to set up POSTGRESS_PASSWORD (or PGPASSWORD referring to the docs) environment variable and use its value as password inside your config/database.yml file.

Also sharing your config/database.yml file would help.

I use Docker for development and once in a while I run into this sort of errors.

Edit: Also try to run rake db:reset after making this changes.