Champer Wu
Champer Wu

Reputation: 1271

Postgres in Rails5 Console

I opened the rails console and called the DatabaseTable, But it failed. I followed

psql: could not connect to server: No such file or directory (Mac OS X)

I tried to open postgres and I got the error.

$ psql

psql: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

and I called the log to observe what happened.

$ tail -f /usr/local/var/postgres/server.log

FATAL:  database "ror_development" does not exist
ERROR:  database "ror_development" already exists
STATEMENT:  CREATE DATABASE "ror_development" ENCODING = 'utf8'
ERROR:  database "ror_test" already exists
STATEMENT:  CREATE DATABASE "ror_test" ENCODING = 'utf8'
FATAL:  database "harem_backend_development" does not exist
LOG:  received smart shutdown request
LOG:  autovacuum launcher shutting down
LOG:  shutting down
LOG:  database system is shut down

I can't understand what happened to my sql, and how to fix it

Upvotes: 2

Views: 2017

Answers (2)

Tom Aranda
Tom Aranda

Reputation: 6026

Make sure postgres is running on MacOS.

If you installed postgres using Homebrew, you should be able to start it using brew:

brew services start postgresql

Otherwise, you can start it with:

pg_ctl -D /usr/local/var/postgres start

For more info on how to install and use postgres in MacOS, read this guide.

Setup your Rails database (Rails >= 5)

Once postgres is started, you will be able to setup your Rails database.

To create the database:

 rails db:create

To migrate the database:

 rails db:migrate

To seed the database:

 rails db:seed

To create and seed the database:

 rails db:setup

Finally, to drop (delete) the database:

 rails db:drop

For Rails < 5, replace rails with bundle exec rake in the above commands.

For more info on setting up and configuring a database with Rails, read the Rails Guide.

Upvotes: 1

Shani
Shani

Reputation: 2541

From the log messages it seems your Postgres is not running(It seems to like postgres process is manually killed.) . Try starting Postgres again

pg_ctl -D /usr/local/var/postgres start
psql

Upvotes: 1

Related Questions