ActiveRecord connection not established - ruby-on-rails

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

Related

ActiveRecord adapter not found

I switched from sqlite to mysql in my rails 3 application. I'm getting the following error when I try to run the server.
Users/harshkumar/.rvm/gems/ruby-2.3.7/gems/activerecord-3.2.21/lib/active_record/connection_adapters/abstract/connection_specification.rb:133:in 'establish_connection': database configuration specifies nonexistent mysql2 adapter (ActiveRecord::AdapterNotFound)
I have included mysql2 gem in the gemfile. My rails version is 3.2.21 and mysql2 gem version is 0.3.21
development:
adapter: mysql2
encoding: utf8
pool: 5
database: sys
username: root
password:
host: 127.0.0.1
port: 3306
test:
adapter: mysql2
database: sys
encoding: utf8
pool: 5
username: root
password: ''
production:
adapter: mysql2
database: sys
encoding: utf8
pool: 5
username: root
password: ''
This is my database.yml file
update: I got this error when I fixed my pervious problem with this solution:
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead

How do I setup username and password on postgresql database.yml for ruby on rails?

I'm trying to start an application on ruby on rails with postgresql but I'm fairly new and unfamiliar with the process. I'm running into a password error when I try to launch the rail server on localhost:3000. Here is the error I'm getting.
fe_sendauth: no password supplied
# connected server's characteristics.
def connect
#connection = PGconn.connect(#connection_parameters) <!-- This line says there's an issue -->
configure_connection
rescue ::PG::Error => error
if error.message.include?("does not exist")
Anyone know how I can set up the username and password on windows or what I need to do to get around this error?
config/database.yml
development:
adapter: postgresql
encoding: utf8
pool: 5
username: postgres
password: postgres_password
database: development_db
production:
adapter: postgresql
encoding: utf8
pool: 5
username: postgres
password: postgres_password
database: production_db

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'

Unable to connect to local PostgreSQL database

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

Can't connect to MySQL server on 'localhost' (10061) while creating DB using rake command

i get this error when i try to create a database using rake command.
this is my database.yml file
# MySQL. Versions 4.1 and 5.0 are recommended.
#
# Install the MYSQL driver
# gem install mysql2
# Ensure the MySQL gem is defined in your Gemfile
# gem 'mysql2'
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: SIMS_development
pool: 5
username: root
password:
host: localhost
port: 3306
# Do not set this db to the same as development or production.
test:
adapter: mysql2
encoding: utf8
reconnect: false
database: SIMS_test
pool: 5
username: root
password:
host: localhost
port: 3306
now i am not sure why i am getting this error.
I think you have old mysql server. So please install new one if you have old version.
Substitute host: localhost with host: 127.0.0.1
you can use socket connection instead of a port for your connection, you can use of this form, it's very easy
for Linux:
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: your_database_name
pool: 5
username: your_username_db
password: your_password_db
socket: /var/run/mysqld/mysqld.sock
for MAC
development:
adapter: mysql2
encoding: utf8
reconnect: false
database: your_database_name
pool: 5
username: your_username_db
password: your_password_db
socket: /tmp/mysql.sock
I hope this method will serve

Resources