I'm trying to rerun an old rails project in my local environment. The frontend is working but I'm having errors on creating the database.
$ rails db:create
rails aborted!
PG::ConnectionBad: could not translate host name "db" to address: nodename nor servname provided, or not known
database.yml
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV.fetch("DB_HOST") { "localhost" } %>
username: <%= ENV.fetch("DB_USERNAME") { "postgres" } %>
password: <%= ENV.fetch("DB_PASSWORD") { nil } %>
timeout: 5000
development:
<<: *default
database: sample_development
test:
<<: *default
database: sample_test
production:
<<: *default
database: sample_production
I am trying to create database using default user 'postgres'
but while executing rails db:create not seleting role name postgres, it's selecting my systme name 'mysystem'.
below is my database.yml file code
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: user_development
username: <%= ENV['DATABASE_USERNAME'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
test:
<<: *default
database: user_test
production:
<<: *default
host: <%= ENV['DATABASE_HOST_URL'] %>
database: user_production
username: <%= ENV['USER_DATABASE_USERNAME'] %>
password: <%= ENV['USER_DATABASE_PASSWORD'] %>
Error while executing rails db:create
rails aborted!
ActiveRecord::ConnectionNotEstablished: FATAL: role "mysystem" does not exist
Caused by:
PG::ConnectionBad: FATAL: role "mysystem" does not exist
There is some helpful information here:
https://guides.rubyonrails.org/v4.2.7/configuring.html#configuring-a-database
sections 3.12 and 3.13
I think your issue might be the host: setting. This should either
be /tmp for a unix domain socket, or an ip address / hostname, if used.
Settings I used in testing:
postgresql 12.3 debian unix.
Using this pg_hba.conf:
local all all ident
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
host all all 192.168.50.0/24 md5
You might want a hostssl entry for data being sent between machines.
# using the same "user" database name you have.
rails new user -d postgresql
#setting the password for testing.
psql
alter role user1 password 'testpass';
cd user/config
emacs database.yml
#example using ident authentication - being logged in as the same unix user.
production:
<<: *default
host: /tmp
database: user_production
username: user1
password: nil
# using a host ip address
production:
<<: *default
host: 192.168.50.6
database: user_production
username: user1
password: testpass
# using a host ip address with env vars
#export USER_DATABASE_USERNAME="user1"
#export USER_DATABASE_PASSWORD="testpass"
production:
<<: *default
host: 192.168.50.6
database: user_production
username: <%= ENV['USER_DATABASE_USERNAME'] %>
password: <%= ENV['USER_DATABASE_PASSWORD'] %>
# hardcoding everything in the url for illustration:
production:
<<: *default
url: postgres://user1:testpass#192.168.50.6/user_production
# best practice according to doc. DATABASE_URL has precedence over database.yaml when set
# we are just indicating we know that here.
# export DATABASE_URL="postgres://user1:testpass#192.168.50.6/user_production"
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
user1#debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:create
Created database 'user_production'
user1#debian10 /home/user1/rails/user > RAILS_ENV=production bin/rails db:drop
Dropped database 'user_production'
#Seeing what is being used:
RAILS_ENV=production bin/rails runner 'p ActiveRecord::Base.configurations'
#<ActiveRecord::DatabaseConfigurations:0x0000564e40093fa0 #configurations=[#<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e40092f60 #env_name="default", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400928f8 #env_name="development", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_development"}>, #<ActiveRecord::DatabaseConfigurations::HashConfig:0x0000564e400921a0 #env_name="test", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :database=>"user_test"}>, #<ActiveRecord::DatabaseConfigurations::UrlConfig:0x0000564e40091b60 #env_name="production", #name="primary", #configuration_hash={:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :username=>"user1", :password=>"testpass", :database=>"user_production", :host=>"192.168.50.6"}, #url="postgres://user1:testpass#192.168.50.6/user_production">]>
I am trying to connect to remote mysql server. This is how I set up my connection:-
default: &default
adapter: mysql2
encoding: utf8
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: 13.58.156.50
port: 3306
# username: <%= ENV['FOODCLUBE_DATABASE_USER_DEVELOPMENT'] %>
# password: <%= ENV['FOODCLUBE_DATABASE_PASSWORD_DEVELOPMENT'] %>
username: root
password: Admin123#
socket: /var/run/mysqld/mysqld.sock
variables:
sql_mode: "TRADITIONAL"
development:
<<: *default
database: timer
# food_delivery_development
test:
<<: *default
database: food_club_test
production:
<<: *default
database: food_clube_production
username: <%= ENV['FOODCLUBE_DATABASE_USER_PRODUCTION'] %>
password: <%= ENV['FOODCLUBE_DATABASE_PASSWORD_PRODUCTION'] %>
When I am trying to hit a query from the Rails console, I am getting this error message:-
Mysql2::Error::ConnectionError: Lost connection to MySQL server at 'reading initial communication packet', system error: 0
How can I fix this?
I have created a Rails 5.0.2 app with postgres as the database, it all works fine in the development environment.
But when I try to test the app in the production environment (Still on my local machine) I just can't figure out what the password for the database is.
I see this error in the log:
PG::ConnectionBad (FATAL: Peer authentication failed for user "myapp"
My database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
production:
<<: *default
database: myapp_production
username: myapp
password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>
Can I access the ENV['MYAPP_DATABASE_PASSWORD'] somehow? Was I supposed to set it up when I created the app?
I realize this may be very trivial. Thanks.
I have existing rails project with PostgreSQL DB. When I am running the same in my local machine I am receiving SQLite exception.
But my database.yml configured with postgres adapter only.
Here is the exception:
SQLite3::CantOpenException: unable to open database file
database.yml
postgres: &postgres
adapter: postgresql
encoding: unicode
host: localhost
pool: 5
username: postgres
password: postgres
min_messages: warning
development:
<<: *postgres
database: dev_development
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] || 'postgres' %>
test:
<<: *postgres
database: dev_test
username: <%= ENV['PG_USER'] || 'postgres' %>
password: <%= ENV['PG_PASSWORD'] %>
Perhaps you have an environment variable set (DATABASE_URL) that is overriding your database.yml file? Look at this for more info: http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database
If that's not it, is it possible you recently changed your database.yml file and haven't restarted the server?