I am having an issue while trying to connect with a PG database in my Rails application:
I've create my new app with the following command:
$ rails new -d postgresql myApp
The application was created successfully. Therefore I proceed with the following command:
$ createuser --createdb --login -P myApp
The prompt asks me a password for a new role. I enter the password and re-enter it for confirmation. This the message I get after entering the password:
createuser: could not connect to database postgres: 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"?
Any help? I am using PG version 9.5.3 and Ruby on Rails version 4.2.4
Related
I'm deploying a rails 5 app using Google Cloud SQL(postgresql) & App Engine and I am having issues with the proxy. I followed the tutorial here Ruby on Rails Cloud Sql and setup the proxy successfully.
From my local machine:
psql -h "/cloudsql/[CONNECTION:NAME]" --user [USER] --password
I can see all remote databases and the connections being handled by proxy in the other window. Next I try to run rake db:migrate and get the following 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 "/cloudsql/[CONNECTION:NAME]/.s.PGSQL.5432"?
Here is my database.yml
default: &default
adapter: postgresql
encoding: utf8
pool: 5
timeout: 5000
username: [USER]
password: [USER:PASS]
host: /cloudsql/[CONNECTION:NAME]
Since the proxy is working from the local machine it must be something in my rails app or pg gem. I've reinstall the PG gem with no change. How can get I get my rails app to use the proxy successfully?
Did you activate the Cloud SQL API?
In my case I had forgotten that. Enable that one in your API console. Then it worked for me.
I have an existing Rails app which was previously running successfully on my local laptop (running OSX) with the database stored locally in PostgreSQL. I've also successfully deployed the Rails app to Elastic Beanstalk with the database in RDS - the hosted site is still running.
I haven't touched the localhost version for months and tried to start it today using rails server -e development, but when visiting http://localhost:3000/ as per usual, I get a PG error message
PG::ConnectionBad at / FATAL: password authentication failed for user
"murjfphxxxxxx" FATAL: no pg_hba.conf entry for host "115.x.x.x",
user "murjfphxxxxxx", database "d37vsvehxxxxxx", SSL off
The same error is thrown in the Rails console. It seems that the development version is trying to connect to the AWS database, rather than the localhost database, but I can't figure out why.
/config/database.yml lists the development environment as:
development:
adapter: postgresql
encoding: unicode
database: <my-app>_development
host: localhost
pool: 5
username: postgres
password: <password>
I can connect to the localhost PG database via PSQL and see the contents, so it appears that the localhost PG database is valid.
Why would the development environment try to connect to the AWS version of the database? Please let me know if you need any further information to debug this.
Using ruby-2.3.1, Rails 4.2.0, PostgreSQL 9.6.0.0
You probably have DATABASE_URL in your env. You can check by typing env | grep DATABASE_URL in terminal.
If so, try unsetting it with unset DATABASE_URL.
As said in documentation:
If you have both config/database.yml and ENV['DATABASE_URL'] set then
Rails will merge the configuration together. To better understand this
we must see some examples.
When duplicate connection information is provided the environment
variable will take precedence.
I am using the Cloud9 IDE which has an ubuntu operating system. I recently uninstalled my local SQLlite3 database from my Rails app and successfully installed a local postgres database. I am now trying to pull the postgres production database from Heroku.
If I run ActiveRecord::Base.connection.instance_variable_get(:#config)in Rails Console:
{:adapter=>"postgresql", :encoding=>"unicode", :pool=>5, :template=>"template0", :username=>"ubuntu", :password=>"password", :database=>"learning_app_development"}
From the command line per these instructions:
$ heroku pg:pull DATABASE_URL mylocaldb --app my-app-6503
heroku-cli: Pulling postgresql-meadows-7463 ---> mylocaldb
Password:
After I enter password in this field, it fails:
createdb: could not connect to database template1: FATAL: password authentication failed for user "ubuntu"
This answer by Elliot is the instructions I followed to setup postgres locally.
I solved this with this answer:
heroku pg:pull password authentication failed
from Joel.
It does not make any sense to me all besides that maybe Heroku will not accept password as the password?
I am on a Ubuntu 14.04 LTS machine.
I found useful information on how to configure PostgreSQL in Ubuntu for Rails development at help.ubuntu.com, at Heroku and at digitalocean.com.
Putting everything together, all the information seems to converge on the necessity of creating a database superuser with login name that match my Ubuntu user name with: sudo -u postgres createuser --superuser $USER
When time arrives to create a password for the new superuser with sudo -u postgres psql , I am wondering if Rails can use PostgreSQL without setting the password, if this password can and should be different from my Ubuntu account password and also whether database.yml could be a security concern when pushing to Git repository hosting web sites and to Heroku. Indatabase.yml in fact is recorded exactly this kind of sensitive information.
According to Heroku it is necessary "to export the DATABASE_URL environment variable for your app to connect to it when running locally", with: export DATABASE_URL=postgres:///$(whoami)
Is that really necessary? At help.ubuntu.com and digitalocean.com this information is not reported.
Finally I am wondering whether the choice of installing PostgreSQL through the PostgreSQL apt repository would be safe enough or it would be preferable to install the LTS version of Ubuntu.
There are two ways for Rails to set the connection with a database: via config/database.yml or via the environment variable ENV['DATABASE_URL']. See at guides.rubyonrails.org
By default $DATABASE_URL is empty:
echo $DATABASE_URL
If posgresql is installed via PostgreSQL apt repository, in order for Rails to use the pg gem it is also necessary to install the libpq-dev package, otherwise bundle install will fail. See Can't find the 'libpq-fe.h header when trying to install pg gem.
From 'man createuser':
createuser creates a new PostgreSQL user (or more precisely, a role). Only superusers and users with
CREATEROLE privilege can create new users, so createuser must be invoked by someone who can connect as a
superuser or a user with CREATEROLE privilege.
When postgresql is installed, it creates a user postgres with role postgres. It also creates a postgres system account.
So this is why createuser should be run as postgres user in order to connect to postgresql for the first time and add the user $USER (current system user).
It is not necessary to create a password for the new database user. Most people add database.yml to their .gitignore file so it stays out of version control. It is also possible to use .pgpass to keep sensitive
information out of the *.yml file: see postgresql documentation.
It is possible to connect to postgresql only as a database user AND through an existing database.
During installation from the postgresql apt repository, postgresql only creates the postgres user and the postgres database.
The psql command allows the current user to connect to the postgresql database named after the current user. So, if the system user is 'dave' AND there is a 'dave' database it is possible for 'dave' to connect to the 'dave' database with command psql with no options.
If 'dave' is a database user but the database 'dave' was not created, for dave to connect to postgresql it is necessary to specify an existing database with:
psql -d postgres
Alternatively, it is possible for dave to connect to postgresql executing the psql command as the postgres user with sudo:
sudo -u postgres psql
I was using sqlite but switched to pg for some reasons.
I included pg gem in the makefile and made changes in the config/environments.
When I started migrating the data using rake db:migrate , I’m getting this 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 "/var/run/postgresql/.s.PGSQL.5432"?
I've gone through all the posts related to this but did not find a solution.
I'm using ruby 2.1.5 and rails 4.2.0.
sudo /etc/init.d/postgresql start
or
sudo /etc/init.d/postgresql restart
Both should work just fine
If you still get an error you should fix the config/database.yml file one way to go about this is simply create a new project
rails new yourapp -d postgresql
then just copy the database.yml file
if you need to create an new user and password
sudo -u postgres createuser john -s
If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password john
Most of this is from gorails
You need to start your PG server:
$ postgres -D /usr/local/pgsql/data
http://www.postgresql.org/docs/9.1/static/server-start.html