Trying to deploy my rails app to Heroku for the first time, I ran into many problems. The app crashed, and you can look at the logs in the image: http://i.stack.imgur.com/bsx1b.png
Even worse, when I tried to look at my application locally, it failed to work on postgreSQL environment.It worked fine when I went back to use sqLite3 in database.yml. I thought the problem might be that I actually have to install postgreSQL, in addition to adding pg gem and running bundle install.
I went ahead to install PostgreSQL with the one-click installer (Windows 7 64bit). After reboot, a simple rails server or bundle install commands started failing, so I reinstalled the entire ruby & rails.
Now rails server command works fine, and it prompts a different error when I try to look at my app on local environment. So right now I have pgAdmin III and Rails
PG::Error
fe_sendauth: no password supplied
I tried to follow this, but I couldn't find "pg_hba.conf." I guess the answer was based on a different OS.
And now again, the bundler is not working, giving me errors...
Questions:
1) Was I right to install postgreSQL with the one-click installer? Because this caused my ROR to "crash" somehow, and I had to reinstall rails altogether. 2) I think I am going to delete everything related to postgreSQL, reinstall Rails, and start everything from beginning. What are the steps that I have to take? All the references I've looked at do not seem to fit Windows environment.
In my gemfile, I have
gem 'pg'
gem 'thin'
In my database, I have
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: utf8
database: mangfeel_development
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: postgresql
encoding: utf8
database: mangfeel_test
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: mangfeel_production
pool: 5
timeout: 5000
Errors I'm getting at this moment, after installing PostgreSQL:
c:\ruby\myapp>rails server
C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:926:in `report_activate_error': Could not find RubyGem railties (>= 0) (Gem::LoadError)
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:244:in `activate_dep'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:236:in `activate'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:1307:in `gem'
from C:/RailsInstaller/Ruby1.9.3/bin/rails:18:in `<main>'
When I run bundle install:
Gem::InstallError: The 'json' native gem requires installed build tools
Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
An error occurred while installing json (1.7.5), and Bundler cannot continue.
Make sure that `gem install json -v '1.7.5'` succeeds before bundling.
So I will just remove pgAdminIII, PostgreSQL, Ruby on Rails entirely, and start from scratch.
I thought deploying wasn't going to be this hard, but moving from SQLite3 to PostgreSQL is taking more time and effort than I thought it would. I would really appreciate some help on this problem.
------------------------------------------------------------------------------------------------------------------------
UPDATE: AFTER REINSTALLING EVERYTHING, I'M DOING THE SETUP AGAIN FROM SCRATCH.
I checked that my SQLite3 version app worked fine. So I moved onto setting up postgreSQL. I got the following error when I first set up my postgreSQL. By setting up, I mean editing config/database.yml and installing the gem 'pg' and removing the gem 'sqlite3.' When I tried to connect to localhost:3000, I got the following error:
ActiveRecord::ConnectionNotEstablished
After some research, I found that there could be more steps to setting the db. So I ran the bundle command,
bundle exec rake db:setup
But then in the console, I got the following error when I ran bundle exec rake db:setup.
Couldn't create database for {"adapter"=>"postgresql", "host"=>"localhost", "encoding"=>"utf8", "database"=>"db/myapp_test", "pool"=>5, "timeout"=>5000}
rake aborted!
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
c:/ruby/myapp/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:schema:load => environment
When I tried to connect to localhost:3000, I got the above error again, instead of ActiveRecord::ConnectionNotEstablished.
Question: What did I do wrong, and how do I solve the problem?
If you have error:
PG::Error
fe_sendauth: no password supplied
You have good news, your app can work with PostgreSQL database now. (But you need to configure some thing)
After installing PostgreSQL, you can create new rails app using PostgreSQL by command:
rails new my_app -d postgresql
This is database.yml will be created:
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
You see that, three database has username and password field, this is account user in Postgresql, three database in database.yaml file will created by this user. So you need to provide username and password for it. Default after install Postgresql, it created a user with username is postgresql and password is postgresql. You can use it.
If you want to use other user, just open pgAdmin III and create new user by right click on Login Roles and choose New login role to create new user with password. Then put that username and password to database.yaml file.
After that, all you need now, is find and open pg_hba.conf file to make some configure for app can work with PostgreSQL. I'm not using Windows for a long time, so I'm not sure where it is, but you can find in folder you installed PostgreSQL. Example, if you install on E:\ partition, maybe you can find it in:
E:\PostgreSQL\version\data
After you found it, open and find a line:
# "local" is for Unix domain socket connections only
local all all ident sameuser
Change it to:
# "local" is for Unix domain socket connections only
local all all md5
Then restart your Postgresql database. To create database for app, run rake db:create or rake db:create:all. Now your app can using PostgreSQL database now, so happy :).
I am not an expert on Rails, but here is one of my YAML files for a recent deployment i did on heroku with POSTGRES
development:
adapter: postgresql
host: localhost
database: dbname_development
production:
adapter: postgresql
host: localhost
database: dbname_production
Seems like you are missing a parameter called host in case of postgres deployment
Steps to Move From SQLite3 to PostgreSQL on Windows, for an Existing Rails App to Deploy To Heroku:
In your Gemfile, find gem 'sqlite3' and change it to gem 'pg'. Run bundle install.
Download Postgres from here. Choose the one-click installer and select your OS environment (32 or 64 bit). During installation, do NOT choose to install any extra software it recommends you to install. It will possibly mess up your existing Rails and you will have to reinstall Rails.
Now go to config/database.yml, and change it to the following format. The current one is set for SQLite3 and is missing some fields necessary for PostgreSQL.
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
The adapter is different, and there are fields for username and password. The default username and password is postgresql, but if it doesn't work (it didn't work for me), then go to your pgAdmin, create a new login role to make another username/password (check the option to make it a super user) and use that.
4, At this point, you can create a new app by running
rails new my_app -d postgresql
Run your local server, and try connecting to localhost:3000 to see if the welcome page is showing.
5, If everything is working fine, then all you need to do is create a new PostgreSQL database, and migrate your existing models to it. Run
rake db:create:all
rake db:migrate
Done! Now check if your existing app is working fine locally, and try deploying your app to Heroku.
Related
I'm trying to migrate from SQLite to Postgres so the app deploys correctly on Heroku. But I'm not having any luck.
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I'm new to Postgres but I've tried every suggestion I've come across with no luck. Here's my database.yaml file:
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
host: localhost
username: postgres
password: postgres
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
I believe I have the empty database setup correctly as it is in the Postgres list, but running rake db:migrate fails.
Any help is greatly appreciated.
If you haven't yet run:
sudo service postgresql start
in the cloud9 terminal
then switch to your postgres user and verify that you have the database you specified in your database.yaml created
sudo su - postgres
psql
then in the psql prompt enter the following command to list all available databases
\list
If you need to create a database you can do so by typing
CREATE DATABASE "database_name";
To set the password for the postgres user use
\password postgres
After going through the above steps make sure your database.yaml file has the correct database name, username, and password and try running
rake db:migrate
If the database is setup correctly and the database.yaml file is configured
Try restarting the postgres server
sudo /etc/init.d/postgresql restart
Then run rake db:migrate again
Sources
Cloud9 docs related to setting up PostgresSQL
Cloud9 Forum Post related to setting up PostgreSQL in a Rails app
Stackoverflow post related to changing psql password
Stackoverflow post related to restarting the PostgreSQL server
Alternative
Another way I've managed to push apps that I've started developing with SQLite to heroku, is to move the SQLite gem into the development group in my Gemfile, and add a production group with the heroku dependencies:
group :production do
gem 'rails12_factor'
gem 'pg'
end
Then in development bundle install without the production group:
bundle install --without production
And do normal bundle install when I push to heroku
I'm trying to use rails 4.2.6 to develop an app. I'm trying to use postgres for database. Server starts fine but when I try loading a page it throws this "No connection pool for ActiveRecord::Base" error.
What could it be?
EDIT
The pg gem wasn't working properly. I had to comment it before starting the server and then uncomment it from my GemFile afterwards. I realized that I was using Ruby 2.3 instead of Ruby 2.0 (as intended). I removed ruby 2.3 and set up everything under ruby 2.0 environment. It's now working properly.
I had read somewhere that there were some issues with the 'pg' gem in newer Rails releases, requiring people to use 'gem install pg --pre' instead for installing the gem. I tried that, but then my app was requiring the 'pg' gem in my GemFile and, well, the problem stated above showed up again.
This is how my database.yml file ended up:
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: -------
password: -------
pool: 5
development:
<<: *default
database: myDbName
If you are experiencing this error from a rake task, chances are you are not running the :environment task before your task.
Changing:
task :task_name do
end
to:
task task_name: :environment do
end
Should fix the issue.
This issue arises when the server cannot find the corresponding database it depends on to pull data from.
I had same issue from my end, I updated my version of Sqlite3, and it was higher than the version that the current Puma Server version supports.
I simply had to uninstall the Sqlite3 version, and then install the version supported by the current version of Puma Server.
gem uninstall sqlite3
This will uninstall the updated version of Sqlite3, and then you run the code below stating the version supported by your current server.
gem install sqlite3
Or you can as well open your Gemfile, and then include the version for the database that you are using
gem 'sqlite3', '~> 1.3.6'
N/B: The sqlite3 version is the most current version as at the time of writing this answer
And then run
bundle update
to install the version of the database that you specified.
That's all.
I hope this helps.
For PostgreSQL your database.yml file should look something like that:
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
development:
<<: *default
database: your_db_name
Also make sure that you have the gem installed: in your Gemfile:
gem 'pg'
Finally, restart your server.
Hope that helps
Edit: I almost forgot, make sure you have your PostgresSQL running, check this link for download and setup.
Check the database.yml if all settings are ok. If its the first time and you didn't create the database yet use this command to create the database
rake db:create
If the database already exists try reset the db migrations,
rake db:migrate:reset
Hope it'll solve the problem. Go to rails console and try something to check if its working or not.
I had to simply restart the server for the warning to go away.
I've encountered the same problem when I try to access the Model before creating the DataBase.
Make sure you run rake db:migrate at least once.
If you already run migration then check the Database as mentioned in previous answers.
In my case, the config/database.yml was taking variables from the environment:
# ...
test:
<<: *default
database: <%= ENV.fetch("DB_NAME") %>_test
username: <%= ENV.fetch("DB_USER") %>
password: <%= ENV.fetch("DB_PASS") %>
# ...
The error was coming when the new terminal window where I was running the bundle exec rails spec did not have those variables initialized.
Doing,
$ export DB_NAME=mydb
$ export DB_USER=myuser
$ export DB_PASS=mypass
$ bundle exec rails spec
fixed the issue.
I had to restart the server, and make sure you entered username and password. Create development and test databases
when you are running rails db:migrate in data base rows will be created according to your migration files. you can see schema for further info.
Rails 5
New app
Using Postgresql for both test and development
Specs run, so Rails can connect to Postgresql
But when I started the web app, I got "No connection pool with id primary found."
Ran 'bin/rails db:migrate:reset' and restarted the app. It worked. No idea why.
database.yml:
development:
adapter: postgresql
encoding: unicode
database: rails5_development
pool: 5
username: foo
password: bar
host: localhost
port: 5432
test:
adapter: postgresql
encoding: unicode
database: rails5_test
pool: 5
username: foo
password: bar
host: localhost
port: 5432
I have a Rails app that has been using sqlite3 for the DB. Deployed to Heroku. Then find out that Heroku recommends switching to PostgreSQL. So now I'm trying to switch over without any luck. Was trying to use this Railscasts video for help.
I installed Homebrew. Installed PostgreSQL via Homebrew. After installation, there was no mention of creating a username or password for postgres.
I edited my Gemfile to
gem 'pg'
for both production and development and did bundle install.
I edited my database.yml file to this:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
production:
adapter: postgresql
database: isement_production
encoding: unicode
pool: 5
timeout: 5000
Like Ryan says to do in the video, I try this command:
rake db:create:all
Only to get this error:
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I do some more searching, and see that some tutorials show username and password included in the database.yml file. I then find out how to setup a user for Postgresql
After entering in the command $ createuser joe, I was never given the options that the docs say you'll be asked. Such as "Shall the new role be a superuser? (y/n)" So really not sure if the user was created, but there wasn't any errors either.
So, I'm assuming, after creating the user "joe", I reedited my database.yml file to include the user field I just created:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
username: joe
password:
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
Only to still get the same error of not being able to connect.
I've ran the command
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
to make sure the server is running as well.
Just in case it's needed, when I run
which psql
I receive this:
/usr/local/bin/psql
Is there something that I'm missing? The "database name" part of the database.yml file. This is supposed to be a database already created somewhere, or does this file create the database when I run the rake db:create:all command? I'm assuming the latter, so the name of the database doesn't matter?
Lazy option: Add host: localhost to your database.yml.
Only slightly less lazy option: Uninstall and reinstall the pg gem.
What's going on here? There are a number of ways for Postgres to already exist on your system, and the pg gem will use their pg_config output and build for their needs if you install the gem before installing your own copy of Postgres.
In your case, it was built for the version included with some releases of Mac OS X, which uses a socket file at /var/pgsql_socket.
when you are installing Postgres via Homebrew it will add your username to postgres with an empty password.
Just open a command prompt and type
whoami
Then change your database.yml to your mac user name (whatever is returned from whoami) with empty password.
Maybe add host: localhost
to you database.yml
I'm trying to move my database to a PostgreSQL because I'm putting it up on Heroku.
Followed Railscast #342. Installed PostgreSLQ with its dependencies on my Ubuntu machine. When I installed it I think a user was created. I used this user in my database.yml. It looks like this:
production:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_production
pool: 5
username: jdartland
password:
development:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_development
pool: 5
username: jdartland
password:
test:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_test
pool: 5
username: jdartland
password:
Installed pg gem and the taps gem.
Ran a Bundle install, created the databases with rake db:create:all
Started the taps senatra server with taps server sqlite://db/development.sqlite3 jdartland secure
The server started. And tried to pull the SQL to my new development db through this command.
taps pull postgres://jdartland#localhost/dlrvbtApp1_development http://jdartland:secret#localhost:5000
I then get this error:
Failed to connect to database:
Sequel::DatabaseConnectionError -> PG::ConnectionBad: fe_sendauth: no password supplied
I have tried and tried, created new databases, canhing the .yml, pg_config and so on but I can't get it to work.
This is my first time working with PostgreSQL and Heroku, please give me a hand! :)
Change the user on production to localhost and leave the password blank.
production:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_production
pool: 5
username: localhost
password:
If you're moving your database to Heroku, the whole thing is just a case of connecting your DB to Heroku's PG one, and migrating the data.
Did you receive database details from Heroku?
They basically use Amazon to serve their DB's, and you'll get some credentials to put into your yml file for it. Here is an example of one of our live Heroku apps:
production:
adapter: postgresql
database: ********
pool: 5
username: ****************
password: ****************
port: 5432
host: ec2-54-228-234-250.eu-west-1.compute.amazonaws.com
Ways To Migrate To PostgreSQL (Heroku)
If you're looking to migrate your data from SQLite3 to PostgreSQL, I found a really good tutorial on how to do this here. Only problem is that it's not for SQLite lol
If you're trying to pull down your database from Heroku into a local postgres database, use pg:pull or pgbackups:
https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull
https://devcenter.heroku.com/articles/heroku-postgres-import-export
You should also look into using foreman and a .env file to setup your DATABASE_URL similar to how it's ran on Heroku, for dev/prod parity:
https://devcenter.heroku.com/articles/procfile#developing-locally-with-foreman
Got It working by following another post here on stack. Simply went up one directory, installed taps Gem install Taps. Uninstalled Rack gem gem uninstall rack 4 then Reinstalled it gem install rack --version 1.0.1. Did not do this in my progect directory, just simply in RVM. Then Pulled the database from the same directory. (not from my project directory).
Here is the Whole tread: taps migration failing from sqlite to postgres rails4, ruby 1.9.3
Hope it helps someone with the same problem.
Now just one thing left, Push it to heroku..... We'll see how that goes...hehe
Thanks for all help!
I did the following after installing Postgres locally:
gem install postgres-pr
I changed the database.yml:
development:
adapter: postgresql
encoding: unicode
database: typhoon_development
pool: 5
Then, when I did a rake db:create got:
uninitialized constant PostgresPR::Connection::UNIXSocket
Couldn't create database for {"encoding"=>"unicode", "adapter"=>"postgresql", "database"=>"typhoon_development", "p
ool"=>5}
I'm doing it locally to mimic the heroku environment.
Try to provide hostname and port number in the config.
I used a different gem