Unable to restart Postgres Server - ruby-on-rails

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?

Related

could not connect to server: Connection refused (0x0000274D/10061)

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

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

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.

After OSX 10.8 upgrade postgres can not connect to the server

After a recent upgrade to Mountain Lion, I've run into problems with my brewed postgres install.
$ rake db:create
>rake db:migratecould 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" (fe80::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?
After searching around the interwebs, I haven't yet found a solution to this issue.
I have found a suggested steps to help identify the problem but after following these, I'm not sure how to understand the results or what to do next. Can anyone help?
$ pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
>pg_ctl: another server might be running; trying to start server anyway
>server starting
$ pg_ctl -D /usr/local/var/postgres stop -s -m fast
>pg_ctl: could not send stop signal (PID: 865): No such process
$ pg_ctl status
>pg_ctl: no server running
$ ps auxw | grep post
>myuser 19037 2.6 7.7 4388248 324520 ?? S 7:30AM 19:06.02 /Applications/Postbox.app/Contents/MacOS/postbox-bin -psn_0_917728
>myuser 54897 0.1 0.0 2432768 464 s000 R+ 1:47PM 0:00.01 grep post
$ cat /usr/local/var/postgres/server.log
>FATAL: lock file "postmaster.pid" already exists
>HINT: Is another postmaster (PID 821) running in data directory "/usr/local/var/postgres"?
Happy to provide any additional info that may be useful.
try removing:
/usr/local/var/postgres/postmaster.pid
This once worked for me.

rails app use postgresql shows PG::Error

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.

Resources