PG::ConnectionBad - could not connect to server: Connection refused on Mac OS X - ruby-on-rails

After installing the needed ruby version with: rvm install 2.7.1 and installing all the gems,
I was planning to create the db with rails db:create but I am getting this error:
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5442?
Couldn't create 'development' database. Please check the configuration.
rails aborted!
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5442?
My database.yml
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 15 } %>
host: <%= ENV.fetch("DATABASE_HOST") { '127.0.0.1' } %>
development:
<<: *default
database: development
test:
<<: *default
database: test
production:
<<: *default
database: production
Seems that it's related to postgres.
It's version is: postgres (postgreSQL) 12.4
pg gem version is 1.2.3
Restarting the postgresql didn't helped

It appeared that I needed to create a new PostgreSQL database cluster.
First you need to remove the postgres dir in the /usr/local/var/
you can use this code - cd /usr/local/var/ && rm -rf postgres
after that - create the new postgres folder mkdir postgres (inside /usr/local/var)
and initialise the creation of the new PostgreSQL database cluster with -
cd && initdb --locale=C -E UTF-8 /usr/local/var/postgres
The last thing, restart the postgres - brew services restart postgresql

Related

Hi I am trying to run rails db:create in WSL Ubuntu and keep getting this error in regards to postgres

This is my first time trying to set up postgres with rails and have tried so many different ways to get this working. Please help!
Error:
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?
Couldn't create 'backend_development' database. Please check your configuration.
rails aborted!
PG::ConnectionBad: 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?
database.yml:
default: &default
adapter: postgresql
encoding: unicode
username: postgres
password: password
host: localhost
# For details on connection pooling, see Rails configuration guide
# https://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
When I run psql i receive this error again:
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"?
Do you have PostgreSQL installed properly?
To check the version
$ postgres --version .
If it's not installed on your system run
$ sudo apt-get install postgresql postgresql-contrib libpq-dev
If you have it installed login with
$sudo su - postgres
Then create a new user after logged in
$ createuser --pwprompt username
now create the database
$ createdb -O username database_name .

PG::Connection bad error

I am using rails 4.2.6 and ruby 2.3 to create an application .The database I am using is postgresql . When I am running the rails s and going to localhost:3000 then the error PG::Connection bad is showing . how to fix it ?
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"?
when you display such an error, postgresql server is already running.
Check PG status;
pg_ctl -D /usr/local/var/postgres status
Response;
pg_ctl: server is running (PID: 377)
/usr/local/Cellar/postgresql/12.1/bin/postgres "-D" "/usr/local/var/postgres"
Kill the process by pid
kill 377
If your postgresql server is running fine, your configuration in rails might be not well set here is an exemple:
file in config/database.yml
default: &default
adapter: postgresql
encoding: unicode
pool: 5
timeout: 5000
development:
<<: *default
host: localhost
username: your_pg_username
password: your_pg_password
port: your_pg_server_port_5432_by_default
database: your_databasename

PostgreSQL: could not connect to server: Connection timed out

I've been trying to make the switch to PostgreSQL (from SQLite). I'm developing in the environment of Cloud9. However, on db migration I get the error message:
PG::ConnectionBad: could not connect to server: Connection timed out.
Is the server running on host "mydomain.c9.io" (IP address) and
accepting TCP/IP connections on port 5432?
I've been reading all sorts of previously asked questions on this topic but without success. Does anyone see what might be going wrong?
Do I perhaps need to makes change to the pg_hba.conf file? The current version of the file:
# Database administrative login by Unix domain socket
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all xxx.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
# Allow replication connections from localhost, by a user with the
# replication privilege.
#local replication postgres peer
#host replication postgres xxx.0.0.1/32 md5
#host replication postgres ::1/128 md5
database.yml:
default: &default
adapter: postgresql
encoding: unicode
host: 'mydomain.c9.io'
pool: 5
username: my_username
password: my_password
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
I have gem 'pg' in the Gemfile and have run bundle install. Also, I have created the database app_development (used sudo service postgresql start, sudo sudo -u postgres psql and then create database "app_development";) as well as the database app_test. And lastly, I created a new user in psql with CREATE USER my_username SUPERUSER PASSWORD 'my_password';.
Using sudo nano /etc/postgresql/9.3/main/postgresql.conf I also set listen_addresses = '*' but that made no difference.
What might be causing the error message?
Are you sure that your cloud 9's has postgres port(5432) open for access? I assume the cloud 9 host is not listening on port 5432. You can check out this link for detais
http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html

rake db:migrate Could not connect to server error

Hey I want to see someone example app. So I downloaded it off github and installed the version of ruby they are using ruby 2.0.0 but when I try to do $ rake db:migrate I get this error:
rake db:migrate
rake aborted!
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?
/home/alain/.rvm/gems/ruby-2.0.0-preview2/gems/activerecord-4.0.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:796:in `initialize'
/home/alain/.rvm/gems/ruby-2.0.0-preview2/gems/activerecord-4.0.0.rc1/lib/active_record/connection_adapters/postgresql_adapter.rb:796:in `new'
...(etc)
Any idea what I should do ?
database.yml
development:
adapter: postgresql
host: localhost
encoding: unicode
database: rentmybike_dev
pool: 5
username: alain
password: obama
test:
adapter: postgresql
encoding: unicode
database: rentmybike_test
pool: 5
# Add the below...
production:
adapter: postgresql
host: localhost
encoding: unicode
database: rentmybike_production
pool: 5
username: alain
password: obama
To install postgres you may use these commands:
sudo apt-get install postgresql-9.1 postgresql-server-dev-9.1
Then you should set password to newly user
sudo passwd postgres
You have to ensure that database configuration parameters(like username, password etc) in config/database.yml are correct.
And then:
rake db:setup
will create databases and restore schemas if any.

can't connect to rds at ec2 server, missing mysql socket

I have an ec2 server and rds server.
and a Ruby On Rails App
connecting to the rds with these settings worked for me in local ENV:
host: myappnameandhash.us-west-2.rds.amazonaws.com
adapter: mysql2
encoding: utf8
reconnect: false
database: mainDb
pool: 20
username: root
password: xxxx
socket: /var/run/mysqld/mysqld.sock
port: 3306
but on my EC2 server I don't have that mysqld.sock file
so i get this error:
FATAL: failed to connect to MySQL (error=Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2))
what do i need to install in order to have the socket?
thanks
update:
I removed the socket definition and the port.
I deploy using capistrano , now i ssh to my server and go to the "current" folder. there i try to run: rake ts:start
and i get the following:
rake aborted!
Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
but i don't even have the socket definition in my database.yml file anymore
Remove the socket and port number from your database.yml file and then try, it will work.
You must specify the "host" of your RDS, and also configure the security groups in the RDS to allow your instance to connect to it.
I know this has been a while, just encountered this issue with my EC2 server connecting to RDS. Would like to share how I resolve this.
First removing socket and port from the database.yml help
#In your database.yml
development:
#some configuration
staging:
#some configuration
production:
adapter: mysql2
encoding: utf8
reconnect: false
pool: 20
host: url.to.rds.server
database: your-database
username: your-username
password: your-password
To run this rake command on production, you need to specify your rails-env, e.g. if you are using production environment, it should be
bundle exec rake ts:start RAILS_ENV=production

Resources