Integrate Postgresql database with Ruby Application - ruby-on-rails

I just started building my very first Ruby on Rails application and in order for me to host it on heroku I have change my database setting in my application. I did just that by changing the database.yml file in my application. This is what I have in it now.
# SQLite version 3.x
# gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter:postgresql
encoding:unicode
database:dezirus_dev
pool:5
host:localhost
username:postgres
password:
port:5432
# 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:postgresql
encoding:unicode
database:dezirus_test
pool:5
username:postgres
password:
host:localhost
port:5432
production:
adapter:postgresql
encoding:unicode
database:dezirus
pool:5
username:postgres
password:
host:localhost
port:5432
When i try to rake the DB this is the error I get.
RAHMAN#IMLDEV1-LT ~/rails-projects/dezirus
$ rake db:migrate --trace
rake aborted!
no such file to load -- pg
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:68:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:66:in `each'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:66:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:55:in `each'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler/runtime.rb:55:in `require'
/usr/lib/ruby/gems/1.8/gems/bundler-1.0.22/lib/bundler.rb:122:in `require'
/home/RAHMAN/rails-projects/dezirus/config/application.rb:7
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
/home/RAHMAN/rails-projects/dezirus/Rakefile:4
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/rake_module.rb:25:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:501:in `raw_load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:82:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:81:in `load_rakefile'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:65:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.9.2.2/bin/rake:33
/usr/bin/rake:23:in `load'
/usr/bin/rake:23
Please note that the database was prebuilt using postgreSQL and i already have my tables, primary and foreign keys set. I'm not sure if maybe this is why its refusing to work or not. Any help would be really appreciated. Thanks

First install PG in your computer, http://www.postgresql.org/download/
Try opening it with PGAdmin3 (DB administrator it comes with) and create a new BD
next in your gem file add this
gem 'pg'
run bundle install at terminal.
and here i send you an example of my DB.yml
development:
adapter: postgresql
encoding: unicode
database: billy
pool: 5
username: postgres
password:
You may need to configure the pg_hba.conf to login without a password in your localhost.

Add gem 'pg' to your gem file and run
bundle install
Then go here http://www.postgresql.org/download/ and install the database to your machine in order to run your app locally.

Hey guys I finally found a solution for the different issues I had. It turns out that cygwin pg gem does not support my version of PostgreSQL, I used the ruby installer and It installed without a flinch. I then used had to add this line of code to the boot.rb file
require 'yaml'
YAML::ENGINE.yamler = 'syck'
The YML file too needs to look something like this.
development:
adapter: postgresql
encoding: unicode
database: dezirus_dev
pool: 5
username: postgres
password:
# 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: postgresql
encoding: unicode
database: dezirus_test
username: postgres
password:
production:
adapter: postgresql
encoding: unicode
database: dezirus
username: sudo
password: *******
I then ran the rake db:migrate command and voila :)

Related

Can't create model on ruby on rails

I'm starting to learn ruby on rails. the problem that i have is that i don't know why i get there while i want to create model
this is the command that i tried
rails generate model Book
and get this error in my ubuntu 16 terminal
/home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/app_loader.rb:40: warning: Insecure world writable dir /opt in PATH, mode 040777
/home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/application/configuration.rb:148:in `rescue in database_configuration': YAML syntax error occurred while parsing /home/android/ruby Tutorial/library/config/database.yml. Please note that YAML must be consistently indented using spaces. Tabs are not allowed. Error: (<unknown>): found a tab character that violate intendation while scanning a plain scalar at line 17 column 10 (RuntimeError)
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/application/configuration.rb:131:in `database_configuration'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/railtie.rb:122:in `block (2 levels) in <class:Railtie>'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/lazy_load_hooks.rb:43:in `instance_eval'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/lazy_load_hooks.rb:43:in `execute_hook'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/lazy_load_hooks.rb:50:in `block in run_load_hooks'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/lazy_load_hooks.rb:49:in `each'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/lazy_load_hooks.rb:49:in `run_load_hooks'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/base.rb:324:in `<module:ActiveRecord>'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activerecord-5.0.1/lib/active_record/base.rb:24:in `<top (required)>'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `block in require'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:259:in `load_dependency'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/activesupport-5.0.1/lib/active_support/dependencies.rb:293:in `require'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:345:in `active_record_configured?'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:263:in `disconnect_database'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:97:in `preload'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:143:in `serve'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:131:in `block in run'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:125:in `loop'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application.rb:125:in `run'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/spring-2.0.0/lib/spring/application/boot.rb:19:in `<top (required)>'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /home/android/.rbenv/versions/2.3.3/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
i also tried this
rails script/generate model Book
and i get this error:
/home/android/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/railties-5.0.1/lib/rails/app_loader.rb:40: warning: Insecure world writable dir /opt in PATH, mode 040777
rails aborted!
Don't know how to build task 'script/generate' (see --tasks)
/home/android/ruby Tutorial/library/bin/rails:9:in `require'
/home/android/ruby Tutorial/library/bin/rails:9:in `<top (required)>'
/home/android/ruby Tutorial/library/bin/spring:14:in `<top (required)>'
bin/rails:3:in `load'
bin/rails:3:in `<main>'
(See full trace by running task with --trace)
//Edit
I check my database but and also check yamlint website also has error on line 17 but there is no error
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: mysql
pool: 5
timeout: 5000
development:
adapter: mysql
database: library_development
username: root
password: root1234
host: localhost
test:
adapter: mysql
database: library_test
username: root
password: root1234
host: localhost
production:
adapter: mysql
database: library_production
username: root
password: root1234
host: localhost
The main issue is of your database.yml file. You have a tab space in the file whereas the YAML files doesn't allow that since they are treated differently by different editors and tools. And the indentation is the most critical part to proper interpretation of YAML. So on line 17 fix that. And for a permanent solution keep the settings of your text editor according to that. I use Sublime in which on each page I have an option of Convert Indentation to Spaces and in its configuration I can set "translate_tabs_to_spaces": true.
Regarding the second command you were trying:
rails script/generate model Book
It belongs to Rails 2 version. Instead use the new command only:
rails generate model Book
You're using a command for Rails 2 on Rails 5.
To generate a model in Rails 5 you should use:
rails generate model Book
Check if your config/database.yml is valid using http://www.yamllint.com/
Check in your application database file.It is in /config/database.yml.If you dont have the file then you can copy from dtabase.yaml.exemple from same directory.
It is your spacing, it looks like you have 3 spaces instead of 2 in your development, test and production sections.
2 spaces:
default: &default
adapter: mysql
pool: 5
timeout: 5000
3 spaces:
development:
adapter: mysql

how to stop the install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` on the server

I'm deploying a app on production mode but when i try to finish the process the aways show this need for sqlite adapter, please someone know how to stop this issue?
i've been using RAILS_ENV=production but in this case did not worked.
current$ rails generate admin_interface:setup RAILS_ENV=production
DEPRECATION WARNING: Support for Rails < 4.1.0 will be dropped. (called from warn at /home/ubuntu/.rbenv/versions/2.2.2/lib/ruby/2.2.0/forwardable.rb:183)
/home/ubuntu/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/bundler-1.12.5/lib/bundler/rubygems_integration.rb:322:in `block in replace_gem': Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.) (LoadError)
from /home/ubuntu/vitrineonline/releases/10/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/sqlite3_adapter.rb:3:in `<top (required)>'
from /home/ubuntu/vitrineonline/releases/10/vendor/bundle/ruby/2.2.0/gems/backports-3.6.8/lib/backports/std_lib.rb:9:in `require'
from /home/ubuntu/vitrineonline/releases/10/vendor/bundle/ruby/2.2.0/gems/backports-3.6.8/lib/backports/std_lib.rb:9:in `require_with_backports'
from /home/ubuntu/vitrineonline/releases/10/vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
from /home/ubuntu/vitrineonline/releases/10/vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
from /home/ubuntu/vitrineonline/releases/10/vendor/bundle/ruby/2.2.0/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
from /home/ubuntu/vitrineonline/releases/10/vendor/bundle/ruby/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/connection_specification.rb:50:in `resolve_hash_connection'
The first issue I see is that you should put the RAILS_ENV declaration at the beginning of your command as this sets that variable for the life of the command.
RAILS_ENV=production bundle exec rails generate admin_interface:setup
This could be the root cause entirely. (Also note the use of bundle exec to be sure that the gemset specified in the Gemfile is properly loaded)
If you want to set it for the shell, run:
export RAILS_ENV=production
So you don't have to manually set it for every command.
The second possibility is that the database adapter is not properly configured. If this is a Ruby on Rails app, the database adapter is defined in config/database.yml. If not specified, it may be defaulting to sqlite3. Make sure you have a properly setup database.yml file.
production:
adapter: postgresql
database: rails4_stack
username: myusername
password: mypassword
pool: 5
timeout: 5000
encoding: utf8
reconnect: false

How to resolve this sqlite issue with my rails app?

I just deployed my app to production on heroku. However, now when I try to switch back to development and run rails c on my command prompt, I get the following error:
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require': cannot load such file -- sqlite3/database
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/sqlite3-1.3.10-x86-mingw32/lib/sqlite3.rb:9:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.7.7/lib/bundler/runtime.rb:76:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.7.7/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.7.7/lib/bundler/runtime.rb:72:in `each'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.7.7/lib/bundler/runtime.rb:72:in `block in require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.7.7/lib/bundler/runtime.rb:61:in `each'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.7.7/lib/bundler/runtime.rb:61:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/bundler-1.7.7/lib/bundler.rb:133:in `require'
from D:/Railstutorials/Taxibol/Platform/iteration2/taxibol_operator/config/application.rb:9:in `<top (required)>'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:146:in `require'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:146:in `require_application_and_environment!'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:68:in `console'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.6/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.1.6/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Following is my database.yml file:
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/test.sqlite3
production:
adapter: postgresql
host: ec2-23-23-210-37.compute-1.amazonaws.com
port: 5432
database: d9qgibt6qabecr
username: ygaayqvpdrfjwa
password: 'my_password'
I am facing the same issue when I want to start my server in dev mode. Where could I be going wrong?
I'd recommend installing Postgres locally so that your development and production environments are the same.
Sometimes when troubleshooting, it's helpful to run your production environment locally because there might be some other settings which you may not notice which are different in production vs. development. Running production locally is the fast way to test that, and for that you'd need Postgres installed locally because that's what you set in database.yml.
This is how to run your production server or production console locally from the terminal:
RAILS_ENV=production rails server
or
RAILS_ENV=production rails console
FYI, a great way for Mac users to install Postgres is with http://postgresapp.com/ because it's easy to get running with CMD+SPACE_BAR (system search using Spotlight) and typing in "postgres".
ran rake db:create and it worked. Though I don't know why. Would be great if someone could explain...

How to configure the database.yml file is using PostgreSQL if using Publify on Heroku?

I am following along to the tutorial at http://fdv.github.io/publify/#publifydemo but all it says is
Rename database.yml.yourEngine as database.yml
Edit database.yml to add your database name, login and password.
So I renamed database.postgresql.yml to just database.yml
And as far as adding a login and password, I did, here is the file
login: &login
adapter: postgresql
host: localhost
username: troll
development:
encoding: unicode
database: typo_dev
username: troll
password: lol
host: localhost:3000
test:
encoding: unicode
database: typo_dev
username: troll
password: lol
host: localhost:3000
production:
database: typo
username: troll
password: lol
host: <hostname or ip address. eg.: localhost>
Not really sure where to go from here. I did search the documentation for PostgreSQL but not sure where to get started in the 2808 pages of the manual.
I did run into this question How to configure Postgresql with rails project? but it did still not run locally
This is the log when I run rails server
jgallardo:bruxir-typo juan.gallardo$ rails server
/Users/juan.gallardo/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/psych.rb:205:in `parse': (<unknown>): found character that cannot start any token while scanning for the next token at line 2 column 3 (Psych::SyntaxError)
from /Users/juan.gallardo/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/psych.rb:205:in `parse_stream'
from /Users/juan.gallardo/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/psych.rb:153:in `parse'
from /Users/juan.gallardo/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/2.0.0/psych.rb:129:in `load'
from /Users/juan.gallardo/Desktop/myfiles/test-projects/typo/bruxir-typo/Gemfile:7:in `eval_gemfile'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/bundler-1.3.5/lib/bundler/dsl.rb:30:in `instance_eval'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/bundler-1.3.5/lib/bundler/dsl.rb:30:in `eval_gemfile'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/bundler-1.3.5/lib/bundler/dsl.rb:9:in `evaluate'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/bundler-1.3.5/lib/bundler/definition.rb:19:in `build'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/bundler-1.3.5/lib/bundler.rb:148:in `definition'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/bundler-1.3.5/lib/bundler.rb:136:in `load'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/bundler-unload-1.0.1/lib/bundler-unload.rb:42:in `with_bundle'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/rubygems-bundler-1.2.2/lib/rubygems-bundler/noexec.rb:53:in `candidate?'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/rubygems-bundler-1.2.2/lib/rubygems-bundler/noexec.rb:70:in `setup'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/gems/rubygems-bundler-1.2.2/lib/rubygems-bundler/noexec.rb:85:in `<top (required)>'
from /Users/juan.gallardo/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:116:in `require'
from /Users/juan.gallardo/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:116:in `rescue in require'
from /Users/juan.gallardo/.rvm/rubies/ruby-2.0.0-p195/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:122:in `require'
from /Users/juan.gallardo/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:9:in `<main>'
Not sure if this is the issue, but host and port should be like this:
host: localhost
port: 3000
I don't think this will work:
host: localhost:3000

In Rails, Couldn't create database for {"adapter"=>"postgresql",

I'm trying to run rake db:create or rake db:setup but I get the following error. It's strange because rake db:create it's working fine from others folders with other Rails projects.
user:project user$ rake db:create
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `initialize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `new'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:991:in `connect'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:260:in `initialize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:27:in `new'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/postgresql_adapter.rb:27:in `postgresql_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:304:in `new_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:323:in `checkout_new_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:265:in `block (2 levels) in checkout'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `loop'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:261:in `block in checkout'
/Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:260:in `checkout'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:162:in `connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:409:in `retrieve_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:107:in `retrieve_connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/connection_adapters/abstract/connection_specification.rb:89:in `connection'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:110:in `rescue in create_database'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.0/lib/active_record/railties/databases.rake:54:in `create_database'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/activerecord- 3.1.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
/Users/user/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
/Users/user/.rvm/gems/ruby-1.9.2-p290/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
/Users/user/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `load'
/Users/user/.rvm/gems/ruby-1.9.2-p290/bin/rake:19:in `<main>'
Couldn't create database for {"adapter"=>"postgresql", "database"=>"project_test", "username"=>nil, "password"=>nil, "min_messages"=>"warning"}
This is my database.yml
development:
adapter: postgresql
database: project_development
username:
password:
min_messages: warning
test:
adapter: postgresql
database: project_test
username:
password:
min_messages: warning
production:
adapter: postgresql
database: project_production
username:
password:
min_messages: warning
Posgree is insatelled on /usr/local/var/postgres. Shouldn't rake try to look it in the same folder instead of /var/pgsql_socket/ ? (I don't have a /usr/local/var/pgsql_socket anyways)
Solved!
Solution was simple to update current installed gems.
Another solution to try is set host: localhost or whatever host you use on development settings on database.yml file.
I installed PG on Mac OS X via Homebrew (I didn't realize it came default in Mac OS X), and had the same problem. I may have made the problem worse by installing the pg gem before installing pg via Homebrew.
I solved this same problem by doing the following:
Make sure that all binaries are pointing to binaries in Cellar directory (/usr/local/Cellar on Mac OS X)
Uninstall pg gem gem uninstall pg
Reinstall with Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config (if you created a rails project with pg setup, this is explained in your database.yml config file)
And it worked!
First, you will need to create your user manually in Postgres, while granting privilege of database creation (AKA createdb) for this user.
To do so, first, open Postgres shell prompt
$ sudo -u postgres psql
Create your user
postgres=# create role Your_DB_username with createdb login password 'YOUR_PASSWORD';
Exit from Postgres shell
postgres=# \q
Do whatever db-related rake task:
$ rake db:create
And it will work!
The answer from zquintana above worked for me with a few small caveats.
My solution (using Homebrew):
If you have the gem installed gem uninstall pg
Remove old copy of postgresql brew uninstall postgresql
Re-install postgresql brew install postgresql
The install notes mention:
To have launchd start postgresql at login:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then to load postgresql now:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres
So run ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
Then launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Now re-install the gem with the Homebrew configs gem install pg -- --with-pg-config=/usr/local/bin/pg_config
Now run your rake db:create
In my case this was all I needed: https://dba.stackexchange.com/a/75221
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
I encountered an identical issue and was able to solve the error by:
using postgresapp.com (recommended by heroku in https://devcenter.heroku.com/articles/heroku-postgresql#local-setup),
adding to config/database.yml
host: localhost
resetting shared memory settings as recommended in troubleshooting section of http://postgresapp.com/documentation:
sudo sysctl -w kern.sysv.shmall=65536
sudo sysctl -w kern.sysv.shmmax=16777216
Also check you don't have an open connection such as a client or an application connected to the instance. If that happen you will see this in the error
Couldn't create database for {"adapter"=>"xxxx", "database"=>"xxxx", "host"=>"localhost", "username"=>"postgres", "password"=>"xxxxx", "pool"=>5, "timeout"=>5000}
rake aborted!
ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: source database "template1" is being accessed by other users
DETAIL: There is 1 other session using the database.
Simple add host: localhost to your database.yml
For more detail please visit rake db:create:all fails to connect to PostgreSQL database
I was able to solve this by making sure that the name of my database in the database.yml file was in the following format: appname_development, appname_test, and appname_production. Here is my database.yml file as an example:
development:
adapter: postgresql
database: parallax_development
username:
password:
test:
adapter: postgresql
database: parallax_test
username:
password:
production:
adapter: postgresql
database: parallax_production
username:
password:

Resources