PostgreSQL: could not connect to server: Connection timed out - ruby-on-rails

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

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.

"could not save history to file "/Library/PostgreSQL/9.6/.psql_history": No such file or directory"

I am building a Rails project with Devise and Postgres.
When trying to migrate, I get this error message:
PG::ConnectionBad: FATAL: password authentication failed for user "kala.wetzler".
I have tried this process a few times: https://www.cyberciti.biz/faq/howto-add-postgresql-user-account/ & after what seems like a success (no error messages) & I \q out, I get this message:
could not save history to file "/Library/PostgreSQL/9.6/.psql_history": No such file or directory.
After this, if I try to migrate again I see that it does not recognize my password (as seen in the first error message listed in this post). Is it not working because of this lack of a file to save to or is that just telling me that this process of assigning a user to the database is not being logged? If it is because of a lack of the file to save to, how do I make sure that file gets created?
My config/database.yml:
default: &default
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: localhost
username: kala.wetzler
password: <%= ENV['LIT_DATABASE_PASSWORD'] %>
development:
<<: *default
database: lit_development
test:
<<: *default
database: lit_test
production:
<<: *default
database: lit_production
username: kala.wetzler
password: <%= ENV['LIT_DATABASE_PASSWORD'] %>
& then in my .env file I have LIT_DATABASE_PASSWORD assigned.
My pg_hba.conf:
TYPE DATABASE USER ADDRESS METHOD
"local" is for Unix domain socket connections only
local all all trust
IPv4 local connections:
host all all 127.0.0.1/32 trust
IPv6 local connections:
host all all ::1/128 trust
Allow replication connections from localhost, by a user with the replication privilege.
local replication kala.wetzler trust
host replication kala.wetzler 127.0.0.1/32 trust
host replication kala.wetzler ::1/128 trust

Postgres database has no password - can't get into pgadmin

I have a rails app using postgres. When I made the app, I never created a password. In my database.yml file, no password is listed. My application works, but I am trying to connect to the db using pg_admin so I can see the tables. When I try to connect, I am given the following:
Server doesn't listen
The server doesn't accept connections: the connection library reports
could not connect to server: Connection refused Is the server running on host
"localhost" (::1) and accepting TCP/IP connections on port 3000?
How can i fix this? If it helps, here is the code from my database.yml file for development:
development:
adapter: postgresql
encoding: unicode
database: supersmart_dev
pool: 5
username: postgres
host: localhost

setting ip address of database on different server

I deployed a sample Rails app to a remote server with postgres hosted on the same server. The database.yml file was like this.
production:
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: localhost
username: mrmann
password: secret
Everything worked fine. I then went into the database.yml file and replaced host: localhost with the ip address of a postgres database on another server host: 178.XXX.XXX like this
production:
adapter: postgresql
encoding: unicode
database: remotepg_production
pool: 5
host: 178.XXX.XXX.XXX #ip address of server with other postgres database
username: mrmann
password: secret
When I restarted postgres on the server with the Rails app, the sample application now gave me the 'something went wrong' page for Rails.
The username and the password for the username are the same on both dbs. Can you suggest what the problem might be? Thanks
Update
These are the settings in pg_hba.conf on the server with the database that I want to connect to.
local all postgres peer
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
Fake Rails app IP: 192.241.XXX.X
Fake Database IP: 192.34.XX.XXX
Do you have access to the database logs? If your connection got as far as attempting to connect to the database, there should be some messages there. My WAG is that you need to add that IP to server's pg_hba.conf file and reload Postgres.
To be more clear, the IP to add to the server's pg_hba.conf is the IP that your application is connecting from, and you should also check the value of the database's listen_addresses setting. If the latter is not set to '*' or to the IP of your application, you'll need to change it in the postgresql.conf and restart the db cluster.
[edit, new information provided]
The line in your pg_hba.conf should be
host all all 192.241.XXX.X md5
Reload (not restart) postgres (eg. pg_ctl reload) and then try connecting again. If it fails, find the db log and see what it shows. If there is no message, then it might be a firewall problem. If the latter is suspected, look for any rules relating to port 5432 in the output of iptables -nvL

rake db:create:all FATAL : password authentication failed

I am trying to follow this railscast tutorial. Now I am in the step, where you create the database with rake db:create:all, but I got this error message :
FATAL: password authentication failed for user aldila
I made the user aldila through pgAdmin, by right-clicking the login role and add user aldila with password.
So, here is my database.yml :
development:
adapter: postgresql
host: localhost
encoding: unicode
database: my_app_development
pool: 5
username: aldila
password: *mypass*
test:
adapter: postgresql
host: localhost
encoding: unicode
database: my_app_test
pool: 5
username: aldila
password: *mypass*
production:
adapter: postgresql
host: localhost
encoding: unicode
database: my_app_production
pool: 5
username: aldila
password: *mypass*
Here is my pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.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.
#host replication postgres 127.0.0.1/32 md5
#host replication postgres ::1/128 md5
Maybe there is something's wrong with the installing of PostreSQL, because if I type
psql --version on my command, this is what I got :
'psql' is not recognized as an internal or external command,
operable program or batch file.
But I have no idea what went wrong. I install it from here and pick download the installer from EnterpriseDB for all supported versions.
I know there are already similar questions like this, but I follow this one and this one, but I still get the same error message. I would be really appreciated, if anyone could help. Thanks
Your postgresSQL password didn't match your database.rb's password.. it depends on what environment you are using..

Resources