could not connect to server: Connection refused (0x0000274D/10061) - ruby-on-rails

I'm using rails to develop a website but i get this error when i try to open my localhost
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?
How to fix it guys

In Windows, search for "services" and search for postgres in the list. Right click and select "Start".

It looks like your postgreql server is not running.
You can try to launch it with :
On Linux :
sudo service postgresql start
On windows : changing XX by your postgresql version run :
net start postgresql-XX
You can also stop using postgres and specify a sqlite database in your config/database.yml file :
changing this line :
adapter: postgresql
by this line :
adapter: sqlite3

If you are using postgreql this issue is due to not starting a server, so you should start the server, one way to do it is to cd to postgresql bin and start it with pg_ctl, here is an example:
cd "C:\Program Files\PostgreSQL\14\bin"
pg_ctl -D "C:\Program Files\PostgreSQL\14\data" start

If you are running postgres in a docker image, be sure to map the container port to the host. You can do this with docker run -p 5432:5432 <container-name> or by adding the following to your docker-compose.yml file:
ports:
- 5342:5342

Related

Flyway connection failing, connecting using psql command line works fine

I have a docker service for postgres that is running fine:
postgres:
restart: always
image: postgres:13.0-alpine
ports:
- "5432:5432"
environment:
- DEBUG=false
- POSTGRES_DB=yola
- POSTGRES_PASSWORD=password123
I can connect fine using terminal like this:
export PGPASSWORD=password123; psql -h localhost -p 5432 -d yola -U postgres
Now when I try and run flyway I get a connection problem.
I tried doing both 0.0.0.0 and localhost and the docker image name yola_postgres_1.
I still get this error running the following command:
docker run --rm -v /path/to/folder/migrations:/flyway/sql flyway/flyway -url=jdbc:postgresql://localhost:5432/yola -user=postgres -password=password123 info
ERROR: Unable to obtain connection from database
(jdbc:postgresql://localhost:5432/yola) for user 'postgres':
Connection to localhost:5432 refused. Check that the hostname and port
are correct and that the postmaster is accepting TCP/IP connections.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- SQL State : 08001 Error Code : 0 Message : Connection to
localhost:5432 refused. Check that the hostname and port are correct
and that the postmaster is accepting TCP/IP connections.
Caused by: org.postgresql.util.PSQLException: Connection to
localhost:5432 refused. Check that the hostname and port are correct
and that the postmaster is accepting TCP/IP connections. Caused by:
java.net.ConnectException: Connection refused (Connection refused)
The Problem
You have PostgreSQL running in one container and Flyway in another, and localhost is per container, so Flyway isn't communicating with PostgreSQL
A Solution
You already map 5432 in PostgreSQL to 5432 on the host, so you can add --network=host to the docker run command for the Flyway container which makes localhost match that of the host
This will make your current set up work, and you could then look into container networking if you want your containers to communicate with each other directly, not through the host

Postgresql version 13 psql: error: could not connect to server: could not connect to server: No such file or directory

I'm currently working with a Rails 6 application and Postgresql. I been having issues when I run rails db:create. This error seem to happen after I updated brew upgrade I get the following error:
rails 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'm running Mac OS Mojave and I updated postgresq to version 13.
$ postgres --version
postgres (PostgreSQL) 13.0
I'm guessing this is why I have issues, I noticed in some of the other posts that running brew postgresql-upgrade-database might be a FIX to this problem, however when I run the command I get:
$ brew postgresql-upgrade-database
Error: No such file or directory # rb_sysopen - /usr/local/var/postgres/PG_VERSION
But I can see the .old file when I navigate to postgres as shown in the image below:
Also, if I tried connecting to psql I get an error
$ psql
psql: error: could not connect to server: 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 also see that the service is running:
$ ps -ef | grep postgres
501 48733 5971 0 1:23PM ttys011 0:00.31 /usr/local/Cellar/macvim/8.2-166_1/MacVim.app/Contents/MacOS/Vim /usr/local/var/postgres/
501 52975 5971 0 1:54PM ttys011 0:00.01 grep postgres
What can the issue be?
this fixed it for me
initdb `brew --prefix`/var/postgres/data -E utf8
and then running
pg_ctl -D /usr/local/var/postgres/data -l logfile start
Follow PostgreSQL with Homebrew on Mac for more detail

Connection refused - connect(2) for "localhost" port 9200 with DigitalOcean

I am using DigitalOcean for my Ruby on Rails app with Elasticsearch. So I am getting
Faraday::ConnectionFailed in PetsController#create
Connection refused - connect(2) for "localhost" port 9200
I am wondering if it is because I am not using localhost on DigitalOcean. I am guessing if I have to change it in my app to start listening to the ip address I am using for DigitalOcean, but am not sure where I should look at.
Can you check the following on digitalocean:
sudo vi /etc/elasticsearch/elasticsearch.yml
Look for:
network.bind_host: localhost
source: https://www.digitalocean.com/community/tutorials/how-to-install-elasticsearch-on-an-ubuntu-vps
also, are you sure elasticsearch is running? The following command should give you a hash as result:
curl localhost:9200
If not: try to start it and try again:
sudo service elasticsearch start
Also, you'll probably want to boot elasticsearch automatically like described here https://www.elastic.co/guide/en/elasticsearch/reference/current/setup-service.html

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?

Postgres stops listening on IP (Azure) / rails

I have a Postgres 9.3.9 running on Azure. I have a rails webserver connecting to it.
I can connect to it fine from the rails server for a while. I connect like this:
me#server:~$ psql -h db.mydomain.com -U myuser mydb
Then a while later (30 mins, or hours - it's random) I can't connect anymore and I need to restart the server (/etc/init.d/postgresql restart) to be able to connect again.
When it stops responding, I can't telnet to port 5432 - it does not respond.
This is what I get with psql:
me#server:~$ psql -h db.mydomain.com -U myuser mydb
psql: could not connect to server: Connection timed out
Is the server running on host "db.mydomain.com" (x.x.x.x) and accepting
TCP/IP connections on port 5432?
This is what I get with a Capistrano deploy:
initialize': could not connect to server: Connection timed out (PG::ConnectionBad)
** [out :: x.x.x.x] Is the server running on host "db.mydomain.com" (x.x.x.x) and accepting
** [out :: x.x.xx] TCP/IP connections on port 5432?
Other points:
When it stops responding, my rails web app still works OK from a browser
As soon as I restart the postgresql server it accepts the connection
It is intermittent, if it isn't responding and I leave it for a while (10 minutes? hours?) it might work the next time

Resources