'Unable to create database with rake db:create command
I had postgresql service running but when I run rake db:create it abort the rake and throws error
ActiveRecord::NoDatabaseError: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: database “chat_development" does not exist
In my database.yml I have
development:
<<: *default
database: chat_development
By running the rake command it should create the database but it didn't. I did a lot research and try out the solutions given but nothing work. I did check on this discussion here but when I run rm /usr/local/var/postgres/postmaster.pid it throws error
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
If I run command ps auxwww | grep postgres it will show /opt/homebrew/opt/postgresql/bin/postgres -D /opt/homebrew/var/postgres
If I remember correctly I had issue with the postgres installation when I was getting started (about 4 weeks ago) and did install using Postgres.app, then I had also installed with brew install postgresql (since I am using mac). I am not sure if there are conflict due to multiple installation.
But just now I uninstalled postgres.app and also ran gem uninstall pg and re-install using command gem install pg -- --with-pg-config=/usr/local/bin/pg_config (ref here)
The issue is still there. I don't know what to do anymore. Glad if someone can help. Thank you!
Solution 1:[1]
Had this issue and never quite figured out why rails was doing this, but below should fix it.
- in terminal
psql template1 CREATE USER <username from config/database.yml if specified> with password '<password from config/database.yml if specified>';ALTER USER <username ...> WITH SUPERUSER;CREATE DATABASE chat_development WITH OWNER = '<username ...>';CREATE DATABASE chat_test WITH OWNER = '<username ...>';
you don't need step 2/3, or the WITH OWNER = '<username>'; unless you specify a user/password in config/database.yml.
Some common mistakes, pay close attention to where I have put single quotes, and don't forget the semi-colons!
Solution 2:[2]
In order to solve this problem, you need progress this in step-by-step fashion.
- Run the postgresql server
- Test creating a sample database using a simple client like psql, which should show whether the server is running and you have the right socket/port connection and user credential with authorization
- Check your application configuration to see if you have the correct connection and user credential setup
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ben Garcia |
| Solution 2 | Yusuf N |
