rails app use postgresql shows PG::Error - ruby-on-rails

I just started a new rails app using
rails new myapp --database=postgresql
Then I generate some static pages:
rails g controller StaticPages home about
When I started the server and visit localhost:3000/static_pages/home, the PG::Error shows up:
PG::Error
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
I didn't change anything else
Any idea how to make postgresql work?

It looks like your postgres server is not running. Have you started it up? If you check your system processes do you see the postgres daemon running?
If you are trying to use rails + postgres, I would recommend checking out this great video by Pete Cooper.

Related

Rails db:create using PostgreSQL return PG::ConnectionBad: could not connect to server: Connection refused

I was following the tutorial to develop using react and rails from the DigitalOcean Tutorial.
I have done
rails new rails_react_recipe -d=postgresql -T --webpack=react --skip-coffee
It created the directory with all the necessary tools - ruby and rails.
Then the next step is to create the database
rails db:create
but it returned me the error below
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
Couldn't create 'rails_react_recipe_development' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:create
(See full trace by running task with --trace)
I have installed postgresql 12 for windows from https://www.postgresql.org/download/
I couldn't find any solution online. Help will be really appreciated. Thanks!
You need to start the Postgres server for it to start accepting connections. Currently, you've probably downloaded Postgres, but not started it.
You could follow instructions here to learn how to start it on Windows: https://tableplus.com/blog/2018/10/how-to-start-stop-restart-postgresql-server.html

connection error to redshift within Apache Airflow

I have installed Apache Airflow on Centos VMware, I have created a connection through Airflow UI to Redshift
Admin --> connections
When i run the adhoc query through Data profiling , i get the below error.
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?
Kindly let me know how to fix this.
The error message says Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432? which seems to indicate that you are trying to connect to Redshift running at localhost:5432, which is unlikely.
Though port 5432 is usually for postgres?
Maybe double check that you've configured your Redshift/postgres connection correctly?

pg_dump: [archiver (db)] connection to database "development" failed: could not connect to server: Connection refused

In trying to push my database from my development.sqlite3 file to my Heroku app, I encountered the following error message:
pg_dump: [archiver (db)] connection to database "development" failed: 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?
pg_restore: [archiver] input file is too short (read 0, expected 5)
psql: 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?
! Heroku client internal error.
! Search for help at: https://help.heroku.com
! Or report a bug at: https://github.com/heroku/heroku/issues/new
I am new to Heroku and can't figure this one out. Hoping it's something simple I'm overlooking.
First off, sqlite isn't intended to be used in production. In fact, Heroku doesn't support using sqlite, so you'll need to switch to Postgres if you want to use your app with Heroku: https://devcenter.heroku.com/articles/getting-started-with-rails4#write-your-app.
The specific error you're getting is because your heroku app is to connect to port 5432, which is utilized by Postgres by default and is not used by sqlite. However, since you don't have a PG server to connect to on localhost, you app is failing with that error.

Unable to restart Postgres Server

For my rails application, every time I try to do rails console, I get this error:
server: Connection refused (PG::ConnectionBad)
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 suspected that this was because my postgres server was down.. So I used the lunchy gem to stop and start my server. However, I still keep getting the same error.
lunchy stop postgres
lunchy start postgres
I also tried this:
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
and got server starting
but when I do this: pg_ctl -D /usr/local/var/postgres status, it does not work because I get a pg_ctl: no server running
Why is this the case?

postgresql could not connect error.

Here is my error.
PG::Error - 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?
could not connect to server: Connection refused
Is the server running on host "localhost" (fe80::1) and accepting
TCP/IP connections on port 5432?
I installed postgresql using homebrew into /usr/local/bin/psql
In order to get it to recognize this version I had to add this to my bash file.
export PATH="/usr/local/bin:/usr/local/sbin:~/bin:$PATH"
When I cd into my rails app and run command
rails s
it starts up with the following
=> Booting Thin
=> Rails 4.0.0.beta1 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
>> Thin web server (v1.5.0 codename Knife)
>> Maximum connections set to 1024
>> Listening on 0.0.0.0:3000, CTRL+C to stop
Then when I try to view the app on http://0.0.0.0:3000 I get the above error
anyone might know why this might happening.
As the message says, the most likely cause of this error is that you are not actually running the postgres server. Normally you'd boot this up with */etc/init.d/pg_ctl,* but since you installed this through brew, just run the following command in a Terminal window
postgres
And the server should boot up. If you don't have this set to automatically start upon launch you'll need to run that command every time after you restart your machine.

Resources