bundle exec rake db:create error - ruby-on-rails

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

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 .

Rails does not consider `DATABASE_URL` in the `test` environment?

I'm using docker-compose to create two services for my Rails application:
web - the actual Rails/web app
db - A postgres container running postgres 11.5
I'm running both services locally with RAILS_ENV=development.
As a one-time manual step I want to The very first time I try to run rake db:create to create the database. However, I get a postgres connection error:
docker-compose up --build --no-start
docker-compose run --rm web bundle exec rake db:create
RAILS_ENV=development bundle exec rake db:create
Created database 'feeder_development'
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: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
Couldn't create 'feeder_test' database. Please check your configuration.
rake 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?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/pg-0.18.4/lib/pg.rb:45:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:692:in `connect'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:223:in `initialize'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `new'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/postgresql_adapter.rb:48:in `postgresql_connection'
/app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:811:in `new_connection'
As you can see, it successfully created the development database, but failed on creating the test database
(The db:create tasks automatically creates test and development databases together even when the environment is development)
Here is my database.yml.
default: &default
adapter: postgresql
encoding: utf8
host: localhost
min_messages: warning
pool: 5
timeout: 5000
test:
<<: *default
database: feeder_test
development:
<<: *default
database: feeder_development
According to the Rails guides, you can override the above config by specifying a DATABASE_URL, which I set as:
DATABASE_URL=postgres://feeder:password123#db/
So from what I can tell -
Rails merges the DATABASE_URL info with the database.yml info when trying to create the development database. All is good.
Rails does NOT consider the DATABASE_URL at all when creating the test database. It uses ONLY the database.yml info, which is why it's looking for a psql connection on localhost.
So - is there a way to get Rails to respect my DATABASE_URL ENV on the test environment?
in your database.yml set url to ENV['DATABASE_URL'] and leave other keys as defaults:
test:
url: <%= ENV['DATABASE_URL'] %>
database: app_test
host: 127.0.0.1
DATABASE_URL='postgresql://192.168.0.2/' rails runner 'puts ActiveRecord::Base.configurations.to_json' | jq '.test'
{
"database": "app_test",
"host": "192.168.0.2",
"adapter": "postgresql"
}
Notice that database name left unchanged but host has been overwritten.
I'm pretty sure you can put ERB into the database.yml file, so I'd suggest changing test to
test:
<<: *default
database: <%= ENV['DATABASE_URL'] || 'feeder_test' %>

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

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.

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

Resources