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

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 .

Related

Logs say "fe_sendauth: no password supplied" but rails console working without problems

I'm trying to deploy rails app with capistrano, nginx and puma. When I visit the app I get errors like this in my production.log:
[11c972c9-c8fb-404f-93c3-9fc614ad815b] ActiveRecord::ConnectionNotEstablished (connection to server at "localhost" (127.0.0.1), port 5432 failed: fe_sendauth: no password supplied
):
When I log to this machine and run bin/rails console it works without any problems, I can create records, query records. Connection to database works without problems.
RAILS_ENV is set to production in /etc/environment
Any idea what could be the problem?
database.yml
default: &default
adapter: postgresql
encoding: unicode
host: localhost
password: <%= ENV['APP_DATABASE_PASSWORD'] %>
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
production:
<<: *default
database: app_production
username: app
pg_hba.conf
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 127.0.0.1/32 scram-sha-256
# IPv6 local connections:
host all all ::1/128 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all peer
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
The solution for me was to delete PostgreSQL completely using,
sudo apt-get --purge remove postgresql\*
Click yes on that pop-up, re-install PostgreSQL using,
sudo apt update
then,
sudo apt install postgresql postgresql-contrib
then,
sudo service postgresql start
Finally, you'll also need to create a database user so that you are able to connect to the database from Rails. First, check what your operating system username is:
whoami
If your username is "stack", for example, you'd need to create a Postgres user with that same name. To do so, run this command to open the Postgres CLI:
sudo -u postgres -i
From the Postgres CLI, run this command (replacing "stack" with your username):
createuser -sr stack
Then enter control + d or type logout to exit.

Rails not able to access Postgresql deployed by Docker

I have a docker container that spins up my Postgres DB, Elasticsearch, and Redis; however, my rails server is unable to connect to the Postgres DB... The server starts, but when it attempts to connect to the DB, I get the error
PG::ConnectionBad (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 Docker launches the DB, it does so on localhost:5432. Via the terminal, I'm able to connect to the DB by running psql -h localhost -U myUser, but running psql results in the same error...
My rails database.yml file is configured to connect to the database at localhost:5432 with myUser, but I cannot figure out why it is giving me this error. It seems like Rails is attempting to connect to my Postgres.app server instead of the server Docker is running.
Here is my database.yml file
default: &default
adapter: postgis // I have tried "postgresql" as well
encoding: unicode
reconnect: true
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
url: <%= ENV['DATABASE_URL'] %>
development:
<<: *default
database: my_db
username: <my username>
password: <my password>
Here is my .env
DATABASE_URL=postgres://***:***#novus-postgis-localdev:5432
My docker container shows the following
ENVIRONMENT:
DATABASE_URL: postgres://<my username>:<my password>#novus-postgis-localdev:5432
PATH:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
...
PORTS:
5432/tcp: 0.0.0.0:5432
5432/tcp: :::5432
Therefore, my Rails environment is supposed to connect to my docker DB server, but it appears to be attempting to connect to my Postgres.app server (which isn't running).
What am I missing here / what is wrong with my configurations that have Rails looking at the wrong server?
FWIW, I'm on macOS and I installed Postgres via the Postgres.app.
When the Postgres.app is running a server, the psql command connects to it.
When the Docker container is running, I am unable to start the Postgres. App server because port 5432 is already used (as expected)
UPDATE
I did find out that my Docker DB container is listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" whereas my Rails is pointing to the socket /tmp/.s.PGSQL.5432. I am unable to figure out how to change either of these to be the same (rails to look at /var/run/postgresql/... or docker to launch socket at /tmp/...
In the Postgresql configuration file, try and enter,
listen_addresses='*';

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

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

Starting the postgres service on Gitlab-CI

I have a Rails5 application that uses Postgres. I'm trying to use the Gitlab CI to run my spec tests when I push up new commits.
I tried setting up my file based on the documentation for configuring the .gitlab-ci.yml file and using the postgres service
I haven't changed my runner, so it's running with whatever default Gitlab uses.
.gitlab-ci.yml
services:
- postgres:9.6
variables:
POSTGRES_DB: galapagos_test
POSTGRES_USER: runner
POSTGRES_PASSWORD: ""
before_script:
# System
- apt-get update -qq
# Ruby
- ruby -v
- which ruby
# Gems
- 'echo ''gem: --no-ri --no-rdoc'' > ~/.gemrc'
- gem install bundler
- bundle install --without production --jobs $(nproc) "${FLAGS[#]}"
# App
- RAILS_ENV=test bundle exec rake db:create
- RAILS_ENV=test bundle exec rake db:migrate
rspec:
script:
- bundle exec rspec
database.yml
My Rails app database.yml is configured to use the same postgres database, username, and password specified above
development: &default
adapter: postgresql
database: galapagos_development
encoding: utf8
host: localhost
min_messages: warning
pool: 2
timeout: 5000
test:
<<: *default
database: galapagos_test
user: runner
password: ""
The Error
When the build runs it keeps failing the rake task with
$ RAILS_ENV=test bundle exec rake db:create
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?
Couldn't create database for {"adapter"=>"postgresql", "database"=>"galapagos_test", "encoding"=>"utf8", "host"=>"localhost", "min_messages"=>"warning", "pool"=>2, "timeout"=>5000, "user"=>"runner", "password"=>""}
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?
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `initialize'
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `new'
/usr/local/bundle/gems/pg-0.20.0/lib/pg.rb:56:in `connect'
Clearly the postgres service is installed (the build logs indicate that 9.6 is installed) but the psql server isn't started.
How can I start the postgres service?
I tried 2 "common" solutions I've seen posted, but they both produce errors as well.
$ /etc/init.d/postgresql restart
/bin/bash: line 69: /etc/init.d/postgresql: No such file or directory
ERROR: Job failed: exit code 1
$ service postgresql start
postgresql: unrecognized service
ERROR: Job failed: exit code 1
Change host: localhost to host: postgres in your database.yml.
A service is another container that is not accessible locally. So you have to access it by it's hostname - the name of the service by default. You could set an optional alias for a service if necessary documentation

bundle exec rake db:create error

Keep getting this when I run bundle exec rake db:create:
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?
Any ideas how to fix this?
Thats probably because of a permission issue. Navigate to /etc/postgresql/<your version of pg>/main/ and open up pg_hba.conf.
There search for a line that'd say
local all postgres peer and replace it to local all all trust.
Then run sudo service postgresql restart in your terminal.
Try this :
It will work
your database.yml should look like this :
development:
adapter: postgresql
encoding: unicode
database: <DATABASE_NAME>
pool: 5
username: <username>
password: <password>
host: localhost
port: 5432
And it will be same for another environments

Resources