Postgres on Rails FATAL: database does not exist

Tyler picture Tyler · Jul 29, 2013 · Viewed 37.5k times · Source

I've reinstalled Postgres (9.2.4) and I'm having trouble getting it set back up with Rails 3.2.11. I did:

brew install postgresql
initdb /usr/local/var/postgres
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start

So now I have

$ psql --version
psql (PostgreSQL) 9.2.4
$ which psql
/usr/local/bin/psql

My database.yml file looks like

development:
  adapter: postgresql
  encoding: unicode
  database: myapp_development
  pool: 5
  username: Tyler
  password:
  host: localhost
  port: 5432

And when I run rake db:create:all then rake db:migrate I get the error:

PG::Error: ERROR:  relation "posts" does not exist
LINE 5:              WHERE a.attrelid = '"posts"'::regclass
                                    ^
:         SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                 pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
          FROM pg_attribute a LEFT JOIN pg_attrdef d
            ON a.attrelid = d.adrelid AND a.attnum = d.adnum
         WHERE a.attrelid = '"posts"'::regclass
           AND a.attnum > 0 AND NOT a.attisdropped
         ORDER BY a.attnum

I have tried to clear out everything related to past db, migrations, etc.

I've deleted the schema.rb, seed.rb, and all files in the migrations folder, and anything else I can think of. But the error referring to "posts" makes me think there is still some old reference to my prior database (which had a table called "posts").

Does anyone know how to troubleshoot this error, when trying to completely reinstall/refresh my database?

Answer

user3112576 picture user3112576 · Dec 17, 2013

I was having a similar problem. I checked different websites and tried what they suggested but didn't work. Then I tried what you suggested. rake db:create:all and rake db:migrate it worked for me. Thank you!