Unable to connect postgres server - ruby-on-rails

When I try to connect server to create database. I am getting this error.
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "host"=>"localhost", "socket"=>"/var/run/postgresql/.s.PGSQL.5432", "pool"=>5, "min_messages"=>"error", "database"=>"rails_contributors_test"}
-- enable_extension("plpgsql")
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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 tried a lot but didn't get success. Looking for help.
Postgres version I have as
which postgres
=> /usr/local/bin/postgres
postgres --version
=> postgres (PostgreSQL) 9.4.4

Try installing Postgres.app on your Mac OSX.
and start the postgres i.e. click on Open psql.
Then, restart the rails server.
After this, it should fix your issue.
Update (If the above does not work)
To fix the issue, remove the PID file. Find the postgres data directory. On Mac OSX using homebrew it is /usr/local/var/postgres/.
To make sure this is the problem, look at the log file (server.log). On the last lines you will see: FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 347) running in data directory "/usr/local/var/postgres"?
If so, rm postmaster.pid
Restart your server. On Mac OSX, do:
launchctl unload homebrew.mxcl.postgresql.plist
launchctl load -w homebrew.mxcl.postgresql.plist

Related

How to enable PostgreSQL in a rails app with termux

I'm trying create a app with ruby on rails in termux, I already installed the necessary gems and packages for the setup, but when doing rails db:create. I get this error
rails aborted!
ActiveRecord::ConnectionNotEstablished: connection to server on socket "/data/data/com.termux/files/usr/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
Caused by:
PG::ConnectionBad: connection to server on socket "/data/data/com.termux/files/usr/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
You need to start a PostgreSQL server manually for your app to be able to connect to.
Install postgresql via pkg install postgresql
See the wiki for sample setup instructions https://wiki.termux.com/wiki/Postgresql

Rake Migrate Not Working

I am in no way a code developer. Our code developer quit, and I was just given the project. So I am trying to learn code while fixing what ever issues happen. This is the issue that I am currently facing. I need to add new columns into two different tables, I did three separate Active Record Migrations, following the direction. When I tried to update the schema.rb with
$ rake db:migrate
I got the following.
[pcms -update to 4.4 (master)]$ rake db:migrate
WARNING: Nokogiri was built against LibXML version 2.9.2, but has
dynamically loaded 2.8.0
I, [2017-07-20T08:57:20.436515 #64621] INFO -- : ** [Raven] Raven 0.13.0
configured not to send errors.
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
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?
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
database.yml
development:
adapter: postgresql
database: pcms_development
host: localhost
test:
adapter: postgresql
database: pcms_test
host: localhost

Does rails server have to be running for you to run rake db: actions?

I've always been confused on how you can executive rake db actions from the terminal. Does it go through rails and then change the db? Or does it go through some environment variable you have on your computer to find the db and then changes it?
I'm asking this because I want to run bundle exec rake db:drop but I keep getting
could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
even though my bundle exec rails s connects me to a server correctly.
You can run the rake db commands with the rails server running
The error says there is no connection happening with the database and solution for this is either restart the db server
(if its postgres)
sudo service postgres restart
or may be you are providing wrong user name and password in database.yml file(this should match with the db user name and password)

Trouble installing postgresql on osx using homebrew

I'm trying to install postgres on a new machine for the time. I initially ran thoughtbot's laptop script to install most of what I'd need for rails development which should have included the postgres installation. Yet when I run a rake db:create for the new rails app I get the following error:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "pool"=>5, "timeout"=>5000, "username"=>"riskpool", "password"=>nil, "database"=>"riskpool_development"}
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"?
I tried uninstalling and reinstalling postgres via homebrew like this and this is the result:
$ brew remove postgres
Uninstalling /usr/local/Cellar/postgresql/9.4.5_2... (3021 files, 40M)
And reinstalling postgres
$ brew install postgres
==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.4.5_2.yosemite.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/postgresql-9.4.5_2.yosemite.bottle.tar.gz
==> Pouring postgresql-9.4.5_2.yosemite.bottle.tar.gz
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall postgresql`
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
https://github.com/Homebrew/homebrew/issues/2510
To migrate existing data from a previous major version (pre-9.4) of PostgreSQL, see:
https://www.postgresql.org/docs/9.4/static/upgrading.html
To load postgresql:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres
WARNING: launchctl will fail when run under tmux.
==> Summary
🍺 /usr/local/Cellar/postgresql/9.4.5_2: 3021 files, 40M
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/9.4.5_2/homebrew.mxcl.postgresql.plist: Operation not permitted
if I run the second command
$ postgres -D /usr/local/var/postgres
FATAL: could not open directory "pg_tblspc": No such file or directory
If I try to find out where postgres is installed:
$ which postgres
/usr/local/bin/postgres
Someone suggested I kill an existing process id that might be interfering:
$ rm /usr/local/var/postgres/postmaster.pid
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
But I get the same error
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"?
Have you tried using this command :
rm /usr/local/var/postgres/postmaster.pid
It appears that this file is blocking postgres from starting up and it would be the reason why you get the below error :
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"?
Have you tried launching the server with root privileges ? i.e.
sudo postgres -D /usr/local/var/postgres
It seems though as the server is not launching due to file permission privileges.
It also seems that the brew postinstall scripts failed, so you may need to initialize the db cluster.
You can use initdb to create a new cluster.
sudo initdb /usr/local/var/postgres9.4
Alternatively, if this is for a development machine, I would recommend just using the postgres app for macosx (simple click and run):
http://postgresapp.com/

rake db:migrate RAILS_ENV=production gives error

When I run the command: rake db:migrate RAILS_ENV=production I get the following error:
rake aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "93.188.167.2" and accepting
TCP/IP connections on port 5432?
So I guess my server is not listening on port 5432. So how do I solve this?
I know I have to edit some file so in addition to the instructions on how to actually edit the file (I guess it is the file named postgresql.conf?) please also tell me where I should find it.
My web application is on this directory at the server: /home/admin/www/webstudio
EDIT: I have already tried my best to follow the instructions given here: http://alexbachuk.com/launch-rails-4-application-with-passenger-and-nginx/

Resources