0

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.

Jason
  • 57
  • 1
  • 7

1 Answers1

0

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

Community
  • 1
  • 1
disc0ninja
  • 68
  • 1
  • 8
  • Still no luck. Is there a chance template0 or template1 became corrupted? Is there any way to reset everything and start from scratch? I've been staring at the same error message for 3 days now. – Jason Jan 27 '17 at 18:14
  • I'm not entirely sure, however I found some more information about it in a different thread about [Clobbered default database in PostgreSQL](http://dba.stackexchange.com/questions/55792/clobbered-default-database-in-postgresql) which appears to state that template0 is protected – disc0ninja Jan 28 '17 at 03:11
  • @Jason This thread recommends dropping clusters using `pg_dropcluster` and reinstalling, over performing a full purge [How to thoroughly purge and reinstall postgresql on ubuntu](http://stackoverflow.com/questions/2748607/how-to-thoroughly-purge-and-reinstall-postgresql-on-ubuntu) Cloud9 development enviornment should be using Ubuntu I believe. – disc0ninja Jan 28 '17 at 03:17
  • @Jason You should also try restarting the PostgreSQL server. I edited my response to include instructions. After logging into my own cloud9 environment today I received the same error you've been getting and restarting fixed it. – disc0ninja Jan 28 '17 at 03:28