FATAL: database "postgres" does not exist - ruby-on-rails

Weird error. All help appreciated.
Here's my database.yml
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: username
password:
test:
adapter: postgresql
encoding: unicode
database: app_test
pool: 5
username: username
password:
production:
adapter: postgresql
encoding: unicode
database: app_production
pool: 5
username: username
password:
When I create the databases manually and try to use them, for example rake db:test:preare I get the following error:
FATAL: database "postgres" does not exist
When I try to drop the databases I get the following errors:
Couldn't drop app_development : #<PG::Error: FATAL: database "postgres" does not exist
>
Couldn't drop app_test : #<PG::Error: FATAL: database "postgres" does not exist
>
Couldn't drop app_production : #<PG::Error: FATAL: database "postgres" does not exist
If I try to create the databases through with rake db:create:all I get the following errors:
app_development already exists
app_test already exists
app_production already exists
So it appears that my database.yml is ok. But for some reason it's looking for a database called postgres when that's not what's in my database.yml.
Any help appreciated.
EDIT:
Here is more from the trace of rake:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"app_production", "pool"=>5, "username"=>"username", "password"=>nil}

[Mac OS X] I've never had any problem with PostGreSQL while using Linux. However, I started working with a MacBook and got the same error as you. So... Probably this is not the solution you were expecting for, but this one app solved all my headaches. I just wanted to share it with you guys.
http://postgresapp.com/

You've not said whether you actually have a postgres database, which is what the error is complaining about.
Presumably not, and this "rake" tool is connecting as user postgres to database postgres so it can create the new databases. Obviously it needs to connect as someone to do anything.
Do take the time to skim through the PostgreSQL manuals and understand how to enable/control logging. It'll serve you well in the long run. I'm guessing there is a debug/log setting for rake/rails too.

I'm using Mac OS X, I had the same problem with python and sqlalchemy.
Just like Flavio suggested, check postgresapp and check How to connect section.
from sqlalchemy import create_engine
engine = create_engine('postgresql://localhost/[YOUR_DATABASE_NAME]')

Related

Rails/PostgreSQL problem: PG::ConnectionBad: fe_sendauth: no password supplied

I'm trying to run my tests for my app but I keep getting both this errors:
PG::ConnectionBad: fe_sendauth: no password supplied
PG::ConnectionBad: FATAL: sorry, too many clients already
I'be read all previous answers and it seems to be, for everybody else, either a missing password or a misconfiguration of the pg_hba.conf file.
This is how my database.yml looks like:
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
encoding: utf8
reconnect: true
host: localhost
username: postgres
password: <password>
development:
<<: *default
database: <dev-db>
host: <dev-host>
username: <dev-user>
password: <dev-pass>
test:
<<: *default
database: <test-db>
I already checked the pg_hba.conf config file and its configured to md5 for all entries listed.
Any clues to what might be the problem?
I'm running PostgreSQL 10, Rails 5.2.3 on Ruby 2.5.5, on a Macbook with Mojave.
Thanks in advance.
Does your postgres user actually have a password set on it? Typically there is no password for the default postgres user, so this is something you would need to have gone out of your way to do; judging by the error message, it sounds like you are using the default posgres user and have not gone out of your way to set a password in the PostgreSQL CLI.
I suggest stripping it down to a very basic default, functional config like:
postgres: &postgres
adapter: postgresql
encoding: utf8
pool: 5
development:
<<: *postgres
database: project_name_development
test:
<<: *postgres
database: project_name_test
Then incrementally add back additional configuration options one at a time, confirming that each one works before adding the next. Env vars also may not be available to database.yml depending on your project configuration and installed gems.

ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist when I try to migrate bd in rails

I cloned a friend's git repo and I'm trying to migrate the db. I started postgres, but when I run rails db:migrate, I keep getting the errors:
Rails Error: Unable to access log file.
and
ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist
I've tried all available solutions online but keep getting the same error. Does anyone know what I'm doing wrong?
Try the following
Setting Up Postgres
Create a Postgres user for the Rails app we'll create in the next step. To do this, switch into the Postgres user:
su - postgres
Then create a user (or a "role", as Postgres calls it):
create role myapp with createdb login password 'password1';
and make sure you have config/database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
The solution for me was:
createuser -s postgres
After I ran that on the command line my rake task to create the DB worked

$development_db_name$ does not exist error even after running db:create and db:migrate and getting report that they exist

So I seemingly successfully installed postgres for production for heroku deployment, but locally rails seems to not find/nor create the database.
When I go to localhost:3000 I get the error:
ActiveRecord::NoDatabaseError
FATAL: database "dimension_development" does not exist Run `$ bin/rake db:create db:migrate` to create your database
When I run $bin/rake db:create I get:
~ already exists
Then, run db:migrate and restart the rails server, reload the page, and get same error.
So I tried instead to run $bin/rake db:create:all and got:
~ already exists
dimension_test already exists
dimension_production already exists
Then did db:migrate again but nothing changed.
If they already exist, why isn't rails finding them? I notice that in the config/db folder there is still the old development.sqlite3 file but no postgres file.
Any idea what could be going on to cause this error?
My database.yml is:
development:
adapter: postgresql
database: dimension_development
pool: 5
timeout: 5000
test:
adapter: postgresql
database: dimension_test
pool: 5
timeout: 5000
production:
adapter: postgresql
database: dimension_production
pool: 5
timeout: 5000
Thanks! This has got me stumped and I haven't been able to find anything on stackoverflow dealing with this pitfall.
In case of postgresql you will not find any file like development.sqlite3. It's in case of sqlite only.
You did not provided database credential in your database.yml file might causing this error. Try pass username and password as well.
development:
adapter: postgresql
database: dimension_development
pool: 5
timeout: 5000
username: xxxx
password: xxxx

Rails error creating database with postgres

the first time when postgresql is installed in my local machine i created a user "user1" using the following command :
$ sudo su postgres -c psql
postgres=# CREATE ROLE user1 SUPERUSER LOGIN;
postgres=# \q
then setup my database.yml
development:
adapter: postgresql
encoding: unicode
database: appname_development
pool: 5
timeout: 5000
username: user1
password:
and all work fine, so today i created a new app then i did the same steps (creating a new user "user2" and modify my database.yml)
development:
adapter: postgresql
encoding: unicode
database: application2_development
host: localhost
pool: 5
timeout: 5000
username: user2
password:
but when i try to create the database
$ rake db:create:all
i get this error :
FATAL: Peer authentication failed for user "application2"
i tried to fix the problem by adding host: localhost to my database.yml but now i get another error which is :
fe_sendauth: no password supplied
can someone explains to me what means this error, and why the first time when i created the first user it doesn't show me this error ? then i will be thankful to know the solution as well.
thank you
I have already faced this problem and fixed it.
I think, It will be helpful to you.
please find 'pg_hba.conf' and postgresql.conf files.
generally, pg_hba.conf and postgresql.conf files located at /etc/postgresql/9.1/main/.
put `local all all md5' in pg_hba.conf and save it.
open postgresql.conf file and if listen_addresses = 'localhost' and port = 5432 line are commented. then uncomment to it.
Finally restart to pg server and try to create database.

Heroku Push Errors

Good afternoon,
I browsed the other questions on this topic, and it appears nobody has asked about this issue. I was expecting otherwise!
In any case - I have a PostgreSQL database server running on my local machine with a MYAPP_DEVELOPMENT database. I've tried to do a
heroku db:push
But keep getting the following error:
Failed to connect to database:
Sequel::DatabaseConnectionError -> PGError: FATAL: role "brandon" does not exist
This obviously has something to do with the permissions & users on the local/heroku shared database, but I'm honestly not that good with this kind of stuff. Any help would be appreciated. I'm currently including the "database.yml" file in my slug to Heroku, which has all of the login/password info for my local database... hence why I was not expecting this kind of error.
Thanks!
** EDIT **
Here is the contents of my database.yml file (edited for clarity):
common: &common
adapter: postgresql
encoding: unicode
username: user
password: secret
test:
<<: *common
database: myapp_test
development:
<<: *common
database: myapp_development
production:
<<: *common
database: myapp_production
I figure it must be something on the Heroku setup side. Note that nowhere in my database.yaml file does "brandon" show up. I'm not quite sure where it is pulling that from. My database user name is not that (although that is my name haha)
This error generally means a username has not been specified under the appropriate environment in your database.yml file.
Your database.yml file should resemble the following (I use MySQL locally, hence the adapter and socket parameters):
common: &common
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: <your_username>
password: <your_password>
socket: /tmp/mysql.sock
development:
<<: *common
database: appname_dev
test:
...
staging:
...
production:
<<: *common
database: appname_prod
If that doesn't solve your problem, check to ensure Heroku is running in the correct environment:
heroku config --app <appname>
Ensure RACK_ENV is set to production
If not: heroku config:add RACK_ENV=production --app <appname>
(It would have been helpful if you pasted the contents of your database.yml file.)
You will have to use the Heroku specific ENV var to use the DB which is usually ENV['DATABASE_URL']
Edit: If you have a db mapper like datamapper you'd write it like this:
DataMapper.setup(:default, ENV['DATABASE_URL'] || LOCAL_DB_URL)
which then either uses the Heroku DB if it exists or defaults to your local DB which is stored in the LOCAL_DB_URL var.
Thanks for everyone's input to this one. I ended up fixing my issue by explicitly providing my login details to Heroku during the db:push... e.g.:
heroku db:push postgres://postgres:PASSWORD#127.0.0.1:5432/DATABASE
Not sure why it isn't using the (same) information in my database.yml file, but oh well.

Resources