PostgreSQL rake db:create Error in local environment - ruby-on-rails

I continue to run into issues whenever I use postgreSQL for Rails app development in my local environment.
I'm running on Mac OS X 10.7. I'm aware this version comes with a pre-installed Postgres version.
I've initially installed Postgres using the brew method and have, at least a few times, found success. I'm aware of the user authentication issues.
When punching in psql --version or which psql to determine which installation is being used, I've had to adjust my .bash_profile before to reflect the appropriate path with export PATH=/usr/local/bin:$PATH.
The issue I'm running into now, and hours among hours of Google haven't solved this, is that my machine has restarted without properly closing PostgreSQL. In turn, I am getting the following error when I try to execute rake db:create on a new application, I am getting the following error:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"unicode", "database"=>"app_development", "pool"=>5, "username"=>"tomgeoco", "password"=>nil}
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"?
My config/database.yml looks like:
development:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
test:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
production:
adapter: postgresql
encoding: unicode
database: app_development
pool: 5
username: tomgeoco
password:
When I check the status of Postgres servers with ps auxwww | grep postgre I get the following:
tomgeoco 12596 0.0 0.0 2434892 408 s000 R+ 11:54AM 0:00.00 grep postgres
So what could the problem be in this scenario?

This issue is not about rails but postgres. I faced with exactly the same problem recently with django project.
According to your ps output postgres isn't working at the moment.
First you have to check logs, my one is /usr/local/var/postgres/server.log. It'll give you an idea about what's wrong on postgre starting up.
My problem was solved by fixing PATH variable in .bash_profile.
Try to start your postgre server manually checking all its output. If everything goes fine but broken on system reboot, check out your launchctl daemons to find out which config file is used. This might be a problem as well.
Here you could find good instructions of clear installing of brew version of postgres. And here is modifying of postgres default settings which might be usefull as well.

Related

could not connect to server: No such file or directory (PG::ConnectionBad)

Yesterday wasn't this error. I didn't make any changes in my application.
Just went to sleep and now I get this.
Error:
home/dartnyan/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/
active_record/connection_adapters/
postgresql_adapter.rb:651:in `initialize': could not connect to server:
No such file or directory (PG::ConnectionBad)
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
database.yml
default: &default
adapter: postgresql
pool: 5
imeout: 5000
development:
adapter: postgresql
database: shop_development
pool: 5
username: dartnyan
password: kurt1245
test:
adapter: postgresql
encoding: unicode
database: shop_test
pool: 5
username: dartnyan
password: kurt1245
I found that typing the following into terminal helps:
$ cd /usr/local/var/postgres/
$ rm postmaster.pid
I found this over at http://alumni.lewagon.org/questions/60
It also describes what might be happening. The file may get corrupt if Postgresql was not closed 'gracefully'. Example would be if your battery on your laptop were to die.
On your terminal, please run this command
rm /usr/local/var/postgres/postmaster.pid
or if you have a specific version of postgres:
## version 11 for example
rm /usr/local/var/postgresql#11/postmaster.pid
Looks like the postgres server isn't running. Start the server by running
sudo service postgresql start
I've reinstalled PostgreSQL and now it works.
In my case, this is caused by an update of mac os, my configuration is Ruby 2.6.3 and rails 6.0.3.4 and I need PostgreSQL 12, verify that you have the correct version of PostgreSQL reinstall if you need and finally execute in your project:
gem uninstall pg
bundle install

Cannot connect to Postgresql server, locally

I have a Rails app that has been using sqlite3 for the DB. Deployed to Heroku. Then find out that Heroku recommends switching to PostgreSQL. So now I'm trying to switch over without any luck. Was trying to use this Railscasts video for help.
I installed Homebrew. Installed PostgreSQL via Homebrew. After installation, there was no mention of creating a username or password for postgres.
I edited my Gemfile to
gem 'pg'
for both production and development and did bundle install.
I edited my database.yml file to this:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
production:
adapter: postgresql
database: isement_production
encoding: unicode
pool: 5
timeout: 5000
Like Ryan says to do in the video, I try this command:
rake db:create:all
Only to get this error:
could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I do some more searching, and see that some tutorials show username and password included in the database.yml file. I then find out how to setup a user for Postgresql
After entering in the command $ createuser joe, I was never given the options that the docs say you'll be asked. Such as "Shall the new role be a superuser? (y/n)" So really not sure if the user was created, but there wasn't any errors either.
So, I'm assuming, after creating the user "joe", I reedited my database.yml file to include the user field I just created:
development:
adapter: postgresql
database: isement_dev
encoding: unicode
pool: 5
timeout: 5000
username: joe
password:
test:
adapter: postgresql
database: isement_test
encoding: unicode
pool: 5
timeout: 5000
Only to still get the same error of not being able to connect.
I've ran the command
pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
to make sure the server is running as well.
Just in case it's needed, when I run
which psql
I receive this:
/usr/local/bin/psql
Is there something that I'm missing? The "database name" part of the database.yml file. This is supposed to be a database already created somewhere, or does this file create the database when I run the rake db:create:all command? I'm assuming the latter, so the name of the database doesn't matter?
Lazy option: Add host: localhost to your database.yml.
Only slightly less lazy option: Uninstall and reinstall the pg gem.
What's going on here? There are a number of ways for Postgres to already exist on your system, and the pg gem will use their pg_config output and build for their needs if you install the gem before installing your own copy of Postgres.
In your case, it was built for the version included with some releases of Mac OS X, which uses a socket file at /var/pgsql_socket.
when you are installing Postgres via Homebrew it will add your username to postgres with an empty password.
Just open a command prompt and type
whoami
Then change your database.yml to your mac user name (whatever is returned from whoami) with empty password.
Maybe add host: localhost
to you database.yml

Moving from Sqlite3 to PostgreSQL Rails

I'm trying to move my database to a PostgreSQL because I'm putting it up on Heroku.
Followed Railscast #342. Installed PostgreSLQ with its dependencies on my Ubuntu machine. When I installed it I think a user was created. I used this user in my database.yml. It looks like this:
production:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_production
pool: 5
username: jdartland
password:
development:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_development
pool: 5
username: jdartland
password:
test:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_test
pool: 5
username: jdartland
password:
Installed pg gem and the taps gem.
Ran a Bundle install, created the databases with rake db:create:all
Started the taps senatra server with taps server sqlite://db/development.sqlite3 jdartland secure
The server started. And tried to pull the SQL to my new development db through this command.
taps pull postgres://jdartland#localhost/dlrvbtApp1_development http://jdartland:secret#localhost:5000
I then get this error:
Failed to connect to database:
Sequel::DatabaseConnectionError -> PG::ConnectionBad: fe_sendauth: no password supplied
I have tried and tried, created new databases, canhing the .yml, pg_config and so on but I can't get it to work.
This is my first time working with PostgreSQL and Heroku, please give me a hand! :)
Change the user on production to localhost and leave the password blank.
production:
adapter: postgresql
encoding: unicode
database: dlrvbtApp1_production
pool: 5
username: localhost
password:
If you're moving your database to Heroku, the whole thing is just a case of connecting your DB to Heroku's PG one, and migrating the data.
Did you receive database details from Heroku?
They basically use Amazon to serve their DB's, and you'll get some credentials to put into your yml file for it. Here is an example of one of our live Heroku apps:
production:
adapter: postgresql
database: ********
pool: 5
username: ****************
password: ****************
port: 5432
host: ec2-54-228-234-250.eu-west-1.compute.amazonaws.com
Ways To Migrate To PostgreSQL (Heroku)
If you're looking to migrate your data from SQLite3 to PostgreSQL, I found a really good tutorial on how to do this here. Only problem is that it's not for SQLite lol
If you're trying to pull down your database from Heroku into a local postgres database, use pg:pull or pgbackups:
https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull
https://devcenter.heroku.com/articles/heroku-postgres-import-export
You should also look into using foreman and a .env file to setup your DATABASE_URL similar to how it's ran on Heroku, for dev/prod parity:
https://devcenter.heroku.com/articles/procfile#developing-locally-with-foreman
Got It working by following another post here on stack. Simply went up one directory, installed taps Gem install Taps. Uninstalled Rack gem gem uninstall rack 4 then Reinstalled it gem install rack --version 1.0.1. Did not do this in my progect directory, just simply in RVM. Then Pulled the database from the same directory. (not from my project directory).
Here is the Whole tread: taps migration failing from sqlite to postgres rails4, ruby 1.9.3
Hope it helps someone with the same problem.
Now just one thing left, Push it to heroku..... We'll see how that goes...hehe
Thanks for all help!

Rails: Deploying to Heroku, Many Problems

Trying to deploy my rails app to Heroku for the first time, I ran into many problems. The app crashed, and you can look at the logs in the image: http://i.stack.imgur.com/bsx1b.png
Even worse, when I tried to look at my application locally, it failed to work on postgreSQL environment.It worked fine when I went back to use sqLite3 in database.yml. I thought the problem might be that I actually have to install postgreSQL, in addition to adding pg gem and running bundle install.
I went ahead to install PostgreSQL with the one-click installer (Windows 7 64bit). After reboot, a simple rails server or bundle install commands started failing, so I reinstalled the entire ruby & rails.
Now rails server command works fine, and it prompts a different error when I try to look at my app on local environment. So right now I have pgAdmin III and Rails
PG::Error
fe_sendauth: no password supplied
I tried to follow this, but I couldn't find "pg_hba.conf." I guess the answer was based on a different OS.
And now again, the bundler is not working, giving me errors...
Questions:
1) Was I right to install postgreSQL with the one-click installer? Because this caused my ROR to "crash" somehow, and I had to reinstall rails altogether. 2) I think I am going to delete everything related to postgreSQL, reinstall Rails, and start everything from beginning. What are the steps that I have to take? All the references I've looked at do not seem to fit Windows environment.
In my gemfile, I have
gem 'pg'
gem 'thin'
In my database, I have
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: postgresql
encoding: utf8
database: mangfeel_development
pool: 5
timeout: 5000
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
adapter: postgresql
encoding: utf8
database: mangfeel_test
pool: 5
timeout: 5000
production:
adapter: postgresql
encoding: utf8
database: mangfeel_production
pool: 5
timeout: 5000
Errors I'm getting at this moment, after installing PostgreSQL:
c:\ruby\myapp>rails server
C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:926:in `report_activate_error': Could not find RubyGem railties (>= 0) (Gem::LoadError)
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:244:in `activate_dep'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:236:in `activate'
from C:/Program Files (x86)/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/rubygems.rb:1307:in `gem'
from C:/RailsInstaller/Ruby1.9.3/bin/rails:18:in `<main>'
When I run bundle install:
Gem::InstallError: The 'json' native gem requires installed build tools
Please update your PATH to include build tools or download the DevKit
from 'http://rubyinstaller.org/downloads' and follow the instructions
at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'
An error occurred while installing json (1.7.5), and Bundler cannot continue.
Make sure that `gem install json -v '1.7.5'` succeeds before bundling.
So I will just remove pgAdminIII, PostgreSQL, Ruby on Rails entirely, and start from scratch.
I thought deploying wasn't going to be this hard, but moving from SQLite3 to PostgreSQL is taking more time and effort than I thought it would. I would really appreciate some help on this problem.
------------------------------------------------------------------------------------------------------------------------
UPDATE: AFTER REINSTALLING EVERYTHING, I'M DOING THE SETUP AGAIN FROM SCRATCH.
I checked that my SQLite3 version app worked fine. So I moved onto setting up postgreSQL. I got the following error when I first set up my postgreSQL. By setting up, I mean editing config/database.yml and installing the gem 'pg' and removing the gem 'sqlite3.' When I tried to connect to localhost:3000, I got the following error:
ActiveRecord::ConnectionNotEstablished
After some research, I found that there could be more steps to setting the db. So I ran the bundle command,
bundle exec rake db:setup
But then in the console, I got the following error when I ran bundle exec rake db:setup.
Couldn't create database for {"adapter"=>"postgresql", "host"=>"localhost", "encoding"=>"utf8", "database"=>"db/myapp_test", "pool"=>5, "timeout"=>5000}
rake aborted!
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Connection refused (0x0000274D/10061)
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
c:/ruby/myapp/config/environment.rb:5:in `<top (required)>'
Tasks: TOP => db:schema:load => environment
When I tried to connect to localhost:3000, I got the above error again, instead of ActiveRecord::ConnectionNotEstablished.
Question: What did I do wrong, and how do I solve the problem?
If you have error:
PG::Error
fe_sendauth: no password supplied
You have good news, your app can work with PostgreSQL database now. (But you need to configure some thing)
After installing PostgreSQL, you can create new rails app using PostgreSQL by command:
rails new my_app -d postgresql
This is database.yml will be created:
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
You see that, three database has username and password field, this is account user in Postgresql, three database in database.yaml file will created by this user. So you need to provide username and password for it. Default after install Postgresql, it created a user with username is postgresql and password is postgresql. You can use it.
If you want to use other user, just open pgAdmin III and create new user by right click on Login Roles and choose New login role to create new user with password. Then put that username and password to database.yaml file.
After that, all you need now, is find and open pg_hba.conf file to make some configure for app can work with PostgreSQL. I'm not using Windows for a long time, so I'm not sure where it is, but you can find in folder you installed PostgreSQL. Example, if you install on E:\ partition, maybe you can find it in:
E:\PostgreSQL\version\data
After you found it, open and find a line:
# "local" is for Unix domain socket connections only
local all all ident sameuser
Change it to:
# "local" is for Unix domain socket connections only
local all all md5
Then restart your Postgresql database. To create database for app, run rake db:create or rake db:create:all. Now your app can using PostgreSQL database now, so happy :).
I am not an expert on Rails, but here is one of my YAML files for a recent deployment i did on heroku with POSTGRES
development:
adapter: postgresql
host: localhost
database: dbname_development
production:
adapter: postgresql
host: localhost
database: dbname_production
Seems like you are missing a parameter called host in case of postgres deployment
Steps to Move From SQLite3 to PostgreSQL on Windows, for an Existing Rails App to Deploy To Heroku:
In your Gemfile, find gem 'sqlite3' and change it to gem 'pg'. Run bundle install.
Download Postgres from here. Choose the one-click installer and select your OS environment (32 or 64 bit). During installation, do NOT choose to install any extra software it recommends you to install. It will possibly mess up your existing Rails and you will have to reinstall Rails.
Now go to config/database.yml, and change it to the following format. The current one is set for SQLite3 and is missing some fields necessary for PostgreSQL.
development:
adapter: postgresql
encoding: unicode
database: my_app_development
pool: 5
username: my_app
password:
test:
adapter: postgresql
encoding: unicode
database: my_app_test
pool: 5
username: my_app
password:
production:
adapter: postgresql
encoding: unicode
database: my_app_production
pool: 5
username: my_app
password:
The adapter is different, and there are fields for username and password. The default username and password is postgresql, but if it doesn't work (it didn't work for me), then go to your pgAdmin, create a new login role to make another username/password (check the option to make it a super user) and use that.
4, At this point, you can create a new app by running
rails new my_app -d postgresql
Run your local server, and try connecting to localhost:3000 to see if the welcome page is showing.
5, If everything is working fine, then all you need to do is create a new PostgreSQL database, and migrate your existing models to it. Run
rake db:create:all
rake db:migrate
Done! Now check if your existing app is working fine locally, and try deploying your app to Heroku.

Configuring Postgres in Ubuntu 9.1 Rails dev-box for Cucumber testing

I'm trying to install Postgres 8.4 for a Rails dev-box and I'm having a couple problems.
I installed postgres and pg-admin3 through apt-get.
Using the lastest Rails 2.3.5 and lastest Ruby 1.9.1
Now the configuration is bothering me. I found some documentation regarding setting up the user for the postgres user(which is the default admin user for postgres I guess), which I did.
sudo -u postgres psql postgres
\password postgres
Prior to that I was getting an error...
FATAL: Ident authentication failed for user
After handling that, I managed to create a database in pg-admin3.
Updated the auto-generated database.yml to look like this.
development:
adapter: postgresql
encoding: unicode
database: rails-box_development
pool: 5
username: postgres
password: ********
host: localhost
port: 5432
After this rake db:create works. If I remove the host and port it doesn't work.
My Question is.. What is the default configuration for those developers that use Rails and Postgres for development? You use that ident stuff or what? What are your steps if you are setting up a new box?
You need to adjust /etc/postgresql/8.4/main/pg_hba.conf (or 8.3 if you are running that). Look here:
http://www.postgresql.org/docs/8.3/static/auth-pg-hba-conf.html

Resources