Unable to connect to local PostgreSQL database - ruby-on-rails

I created a simple rails application:
1
$ rails new myapp1 -d postrgresql
2. Removed /public/index.html
3. Created the Homecontroller and the actions
4. Wrote the routes
myapp1::Application.routes.draw do
root :to => "home#index"
match 'about' => 'home#about'
match 'contacts' => 'home#contacts'
match 'projects' => 'home#projects'
end
5. Created the views and the default layout
6. The bottom line. Here is the database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
test:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
production:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
and of course I created user and database
sudo -u postgres psql
postgres=# \password
postgres=# create user myapp1_user with password '1234';
postgres=# create database myapp1_db owner myapp1_user;
But there are some worries:
1. Now I don't use any database, but in spite of this the application gives an error
PG::Error FATAL: Peer authentication failed for user "myapp1"
2. It seems like rails turns a blind eye to database.yml. Why does it use the user myapp1 instead of myapp1_user?

Whitespace is significant in YAML.
production:
adapter: postgresql
encoding: unicode
...
password: 1234
Should be:
production:
adapter: postgresql
encoding: unicode
...
password: 1234

Provide host and port in the database.yml file, like this:
development:
adapter: postgresql
encoding: unicode
database: myapp1_db
host: localhost
pool: 5
username: myapp1_user
password: 1234
host: localhost
port: 5432
Works like a charm on Ubuntu 12.04 LTS with Rails 3.2.x and PostgreSQL 9.1.x

Related

Staring Rails App With Postgresql - PG::ConnectionBad

I Cannot get the welcome screen not matter what I do, all I get is the connection issue with the postgres db error.
I ran this command to start the app.
rails new -T appName --database=postgresql
Here is my database.yml file:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
adapter: postgresql
encoding: unicode
database: dbname
pool: 5
username: toolshiring
password: toolshiring
host: localhost
port: 5432
production:
<<: *default
database: toolshiring
username: toolshiring
password: <%= ENV['toolshiring'] %>
I used pgAdmin to add a user with full permissions named toolshiring and has the same password. Then created the database with owner toolshiring.
I decided to try something and it seems to have worked for the time being.
I decided to reset the password for the default db 'postgres' to the password 'postgres' as well, and delete the new db that I created....because the wasn't working for some reason.
in my database.yml file i commented everything but this
development:
adapter: postgresql
encoding: unicode
database: postgres
pool: 5
username: postgres
password: postgres
host: localhost

Rails rejects my database.yml with "'postgresql' database is not configured"

I have tried everything I have read on the internet but cannot seem to get it to work. I am trying to set it up on a new project. Here is my database.yml file:
development:
adapter: postgresql
encoding: unicode
database: timetracker_development
pool: 5
username: postgres
password:
host: localhost
test:
adapter: postgresql
encoding: unicode
database: timetracker_test
pool: 5
username: postgres
password:
host: localhost
production:
adapter: postgresql
encoding: unicode
database: timetracker_production
pool: 5
username: timetracker
password:
And the error i get on my web page:
'postgresql' database is not configured. Available: ["development", "adapter", "encoding", "database", "pool", "username", "password", "host", "test", "production"]
I installed postgress via brew and then did the init command and when I type the start command all it says is "server starting". Does this mean it did or didn't start? Anyone know how I can get this to work?
Indenting is significant in YAML.
blah:
blah2: 1
means a different thing to:
blah:
blah2: 1
Here is how your database.yml should look like:
development:
adapter: postgresql
encoding: unicode
database: timetracker_development
pool: 5
username: postgres
password:
host: localhost
test:
adapter: postgresql
encoding: unicode
database: timetracker_test
pool: 5
username: postgres
password:
host: localhost
production:
adapter: postgresql
encoding: unicode
database: timetracker_production
pool: 5
username: timetracker
password:

How to connect to Postgresql from Ruby On Rails App

I am new in Ruby on rails and i am using ruby v 1.9.3 & rails 3.2.12 with window 7
i want to PostgreSQL database connection in my app.
this is my db connection
development:
adapter: postgresql
encoding: unicode
database: mmagap_development
pool: 5
timeout: 5000
host: 127.0.0.1
username: postgres
password: root
test:
adapter: postgresql
encoding: unicode
database: mmagap_test
pool: 5
username: postgres
password: root
production:
adapter: postgresql
host: 127.0.0.1
encoding: unicode
database: mmagap_production
pool: 5
username: postgres
password: root
this connection every time give error
D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in
`initialize': received invalid response to SSL negotiation: - (PG::ConnectionBad)
Please help how to create PostgreSQL database connection in my RoR app.
You can change host: 127.0.0.1 to host: localhost. It should solve the issue.
First install pg gem
gem install pg
your apps database.yml file shoud be like this
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
Now run
rake db:setup
rake db:migrate
and after this run
rails s
Use this link to verify, Is Postgresql installed successfully or not.
DigitalOceanCommunityArticle Click Here
AskUbuntuLink Click Here
Removed host from development config in database.yml,its worked for me.
development:
adapter: postgresql
database: tes
pool: 5
timeout: 5000
username: postgres
password: root
First you should install PostgreSQL and PgAdmin
After installing this thing you will have to add some pic of code ( to connect PostgreSQL to your rails project ) in your project config/database.yml file (before add PostgreSQL code you will have to remove all Sqlite3 code from config/database.yml file).
development:
adapter: postgresql
encoding: unicode
database: database_name
pool: 5
host: localhost
username: your_PostgreSQL_username
password: your_PostgreSQL_password
test:
adapter: postgresql
encoding: unicode
database: database_name
pool: 5
username: your_PostgreSQL_username
password: your_PostgreSQL_password
staging:
url: <%= ENV['DATABASE_URL'] %>
production:
url: <%= ENV['DATABASE_URL'] %>
postgres is default username and password in PostgreSQL database\
after add this pic of code you have to add one line of code in your rails project root directory in Gemfile
gem ‘pg’
and you must have to remove sqlite3 code from Gemfile ( gem 'sqlite3', '~> 1.4' )before adding gem 'pg'

ActiveRecord connection not established

I used this command to create an app rails new guestbook
then i used :-
rails generate scaffold Person name:string
and in routes.rb i unhashed the root :to => 'people#index'
then I wrote in the address bar localhost:3000/people and I got the following error :-
ActiveRecord::ConnectionNotEstablished
ActiveRecord::ConnectionNotEstablished
Rails.root: /home/rudraksha/rbtest/gbook
please answer ASAP
Anyways, the problem is that, connection is unable to establish with database.
Have you configured you database.yml as per you database? If not you have to configure it first.
If you are using mysql, you have to install mysql gem by adding it to your gemfile and running bundle install and configure database.yml accordingly.
Example of database.yml with mysql:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: <db_name>
pool: 5
username: <your_mysql_username>
password: <your_mysql_password>
host: localhost
production:
adapter: mysql
encoding: utf8
reconnect: false
database: <db_name>
pool: 5
username: <your_mysql_username>
password: <your_mysql_password>
host: localhost

generating schema.rb file for two databases

In my rails application I have two databases, I was able to get this up and running with following settings
database.yml
development: &defaults
adapter: mysql
encoding: utf8
database: <Database1>
username: <user_name>
password: <password>
host: localhost
second_development: &defaults
adapter: mysql
encoding: utf8
database: <Database2>
username: <user_name>
password: <password>
host: localhost
test: &defaults
adapter: mysql
encoding: utf8
database: <Database1_test>
username: <user_name>
password: <password>
host: localhost
second_test: &defaults
adapter: mysql
encoding: utf8
database: <Database2_test>
username: <user_name>
password: <password>
host: localhost
But the problem is when running the test cases, I'm using 'mocha', 'guard' and 'notahat-machinist', and when I try to run the test cases, it first runs the schema.rb file. But the problem is it creates the schema from the first test database only
test: &defaults
adapter: mysql
encoding: utf8
database: <Database1_test>
username: <user_name>
password: <password>
host: localhost
this will makes the test related to the second test database 'Database2_test' fail. What would be the workaround for this.
to generate a schema.rb file which has both the database schema.
thanks in advance
The schema.rb generate is define by the connection. So you need use the good connection in your test to have the good schema.

Resources