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'
Related
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
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
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:
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
Rails new app.
The current database.yml is like that:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
I need to edit this for postgresql database.
How can I do this?
Simply:
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
host: localhost
Source: Configuring Rails Applications
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
pool: 5 # not mandatory
timeout: 5000 # not mandatory
host: localhost
port: your postgresql port number (5432 or 5433)
As Zabba said it's
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
As mentioned in the Configuring Rails Applications. But you might want an additional min_messages: WARNING, to get rid of the nasty NOTICE messages postgresql gives you during a migration. So my database.yml entry looks like this
development:
adapter: postgresql
encoding: unicode
database: blog_development
pool: 5
username: blog
password:
min_messages: WARNING
You might be interested in generating new app with postgres default:
rails new myapp --database=postgresql
as mentioned here: https://devcenter.heroku.com/articles/getting-started-with-rails4
development:
adapter: postgresql
encoding: utf8
database: name
username: hading
password: my_db_password
host: localhost # not mandatory
pool: 5 # not mandatory
timeout: 5000 # not mandatory
Simply use
rails new app_name --database=postgresql
or if existing application try
development:
adapter: postgresql
encoding: unicode
database: app_dev
pool: 5
username: username
password: password
host: localhost
Another way is to have the common values in &default and then have individual values for the other environments, which can be based on environment variables:
default: &default
adapter: postgresql
encoding: unicode
port: <%= ENV.fetch("POSTGRESQL_PORT", "5432") %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: <%= ENV['POSTGRESQL_USER_NAME'] %>
password: <%= ENV.fetch("POSTGRESQL_PASSWORD", "myS3cr3tP4ssw0rd") %>
host: <%= ENV['POSTGRESQL_HOST'] %>
development:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-development
host: db
test:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>-test
host: db
production:
<<: *default
database: <%= ENV['POSTGRESQL_DB'] %>
Here all the values can come from environment variables (if you use Docker or Bitbucket Pipelines) or you can have them in your .env files.