Rails not able to access Postgresql deployed by Docker - ruby-on-rails

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='*';

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.

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 .

How to connect postgresql database on remote server rails

I'm trying to connect to a remote server from my local machine. Here's how my database.yml file looks like:
development:
adapter: postgresql
encoding: unicode
database: db_name
username: mac
password: mac
host: 178.XXX.XXX.XXX
port: 5432
pool: 10
timeout: 5000
I edited pg_hba.conf to take my ip address:
local all all trust
# IPv4 local connections:
host all all 35.XXX.XX.XX/32 trust
# IPv6 local connections:
host all all ::1/128 trust
Remote server is hosted on AWS with centos 6 AMI. Below inbound rules are added in it's security group.
But I keep getting below error:
PG::ConnectionBad: could not connect to server: Connection refused
Is the server running on host "<HOST NAME>" (178.XXX.XXX.XXX) and accepting
TCP/IP connections on port 5432?
For logs checked /var/lib/pgsql/9.6/data/pg_log, however no logs are being created.
Running netstat -ntlp tells that server is listening on 5432 port
Am I missing something or doing something wrong here?
please help
Check my answer from yesterday. There is a procedure how find a problem with connecting to the remote PostgreSQL database:
Remote PostgreSQL connection
The error you have posted is not related to Ruby on rails. It is standard problem with wrong PostgreSQL settings. First try psql which should show the same error as you posted
Write a comment if you will have a problem. I bet that the problem is because you forgot set listen_address"'*' in postgresql.conf because you don't mention this in the answer.

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

Rails is not connecting to remote mongo db through SSH tunnelling

I have my mongoid.yml file set up like (eventually production environment will be called staging):
development:
host: staging.domain.com
port: 27018
username: domain
password: passw0rd
database: domain_production
production:
host: localhost
port: 27017
username: domain
password: passw0rd
database: domain_production
I can open up my tunneling like:
ssh deployer#staging.domain.com -L 27018:staging.domain.com:27017
I can open up my mongo shell with
mongo --port 27018
I can run mongod on the remote port but again, none of the entries on the staging server show up. (Do I need to run mongod?)
But when I go to the domain_production I don't see the documents that show up on the website itself. And when I try to start the rails server it hangs. Am I missing a step?
mongod is the mongo database process. It needs be run prior to running the "mongo" shell. mongo is the interactive javascript shell that you use to interact with the mongod process.
mongod picks up the configuration file, and starts listening on port 21017 by default. 21017 + 1000 = 22017 is the port where the http interface shows up. Did i answer your question?

Resources