Switched to PG! DB migration error: PG::ConnectionBad: - ruby-on-rails

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

Related

Not able to connect with PostgreSQL database with Rails app

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

Ubuntu and Rails PostgreSQL setup

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

Trouble installing postgresql on osx using homebrew

I'm trying to install postgres on a new machine for the time. I initially ran thoughtbot's laptop script to install most of what I'd need for rails development which should have included the postgres installation. Yet when I run a rake db:create for the new rails app I get the following error:
Couldn't create database for {"adapter"=>"postgresql", "encoding"=>"utf8", "pool"=>5, "timeout"=>5000, "username"=>"riskpool", "password"=>nil, "database"=>"riskpool_development"}
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"?
I tried uninstalling and reinstalling postgres via homebrew like this and this is the result:
$ brew remove postgres
Uninstalling /usr/local/Cellar/postgresql/9.4.5_2... (3021 files, 40M)
And reinstalling postgres
$ brew install postgres
==> Downloading https://homebrew.bintray.com/bottles/postgresql-9.4.5_2.yosemite.bottle.tar.gz
Already downloaded: /Library/Caches/Homebrew/postgresql-9.4.5_2.yosemite.bottle.tar.gz
==> Pouring postgresql-9.4.5_2.yosemite.bottle.tar.gz
Warning: The post-install step did not complete successfully
You can try again using `brew postinstall postgresql`
==> Caveats
If builds of PostgreSQL 9 are failing and you have version 8.x installed,
you may need to remove the previous version first. See:
https://github.com/Homebrew/homebrew/issues/2510
To migrate existing data from a previous major version (pre-9.4) of PostgreSQL, see:
https://www.postgresql.org/docs/9.4/static/upgrading.html
To load postgresql:
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
Or, if you don't want/need launchctl, you can just run:
postgres -D /usr/local/var/postgres
WARNING: launchctl will fail when run under tmux.
==> Summary
🍺 /usr/local/Cellar/postgresql/9.4.5_2: 3021 files, 40M
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
/usr/local/Cellar/postgresql/9.4.5_2/homebrew.mxcl.postgresql.plist: Operation not permitted
if I run the second command
$ postgres -D /usr/local/var/postgres
FATAL: could not open directory "pg_tblspc": No such file or directory
If I try to find out where postgres is installed:
$ which postgres
/usr/local/bin/postgres
Someone suggested I kill an existing process id that might be interfering:
$ rm /usr/local/var/postgres/postmaster.pid
rm: /usr/local/var/postgres/postmaster.pid: No such file or directory
But I get the same error
psql: 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"?
Have you tried using this command :
rm /usr/local/var/postgres/postmaster.pid
It appears that this file is blocking postgres from starting up and it would be the reason why you get the below error :
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"?
Have you tried launching the server with root privileges ? i.e.
sudo postgres -D /usr/local/var/postgres
It seems though as the server is not launching due to file permission privileges.
It also seems that the brew postinstall scripts failed, so you may need to initialize the db cluster.
You can use initdb to create a new cluster.
sudo initdb /usr/local/var/postgres9.4
Alternatively, if this is for a development machine, I would recommend just using the postgres app for macosx (simple click and run):
http://postgresapp.com/

Rails issues when connection to PostgreSQL database

Attempting to get Rails running with postgresql on mac osx 10.8 to deploy to heroku. I'm new to mac's.
Had lots of trouble just getting PostgrSQL installed so I could have botched up something trying all the potential solutions I could find searching on that subject. Which mainly appeared to be permissions and existing OSX installation. for this issue I'm noticing some things that make me think there is some fundamental directory or version conflict that I'm not sure how to resolve yet. But I could be wrong and it is something else! Any direction is appreciated.
retrieving an error such as this:
$ rake db:create:all
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/pgsql_socket/.s.PGSQL.5432"?
I'm not sure where and why it's looking for the var directory but ~/var appears to be a link (alias) to /private/var (which has empty pgsql_socket & pgsql_socket_alt directories), /usr/local/var doesn't have pgsql_socket directory but does have a postgres directory. both of those directories my admin user has read & write permission from the finder get info dialog.
The above error shown is followed by a long stack in the gems 1.8 directory and finishes with this:
Couldn't create database for {"pool"=>5, "database"=>"blog_development", "password"=>nil, "username"=>"USERNAME", "encoding"=>"unicode", "adapter"=>"postgresql"}
so checking:
$ which psql
/usr/bin/psql
i do remember after installing postgresql i did this:
initdb /usr/local/var/postgres -E utf8
Confirmed:
$ ps aux | grep postgres
username 346 0.0 0.1 2479616 7724 ?? S 3:08PM 0:00.03 /usr/local/bin/postgres -D /usr/local/var/postgres -r /usr/local/var/postgres/server.log
bundle show yields: pg (0.14.1)
$ postgres --version
postgres (PostgreSQL) 9.2.1
echo $PATH does not show any reference to posgresql.
this app is in a /Users/USERNAME/SUBDIR/APP.
Andy ideas on where i've messed up and how i can get this stuff up and running? I'd appreciate any direction. I'll keep working through stack overflow and google discussions; although my stupidity may be unique!
Thanks.
Add
host: localhost
to database.yml

Error creating Rails DB using rake db:create

I'm attempting to get my first "hello world" rails example going using the rails' getting started guide on my OSX 10.6.3 box.
When I go to execute the first rake db:create command (I'm using mysql) I get:
simon#/Users/simon/source/rails/blog/config: rake db:create (in /Users/simon/source/rails/blog) Couldn't create database for {"reconnect"=>false, "encoding"=>"utf8", "username"=>"root", "adapter"=>"mysql", "database"=>"blog_development", "pool"=>5, "password"=>nil, "socket"=>"/opt/local/var/run/mysql5/mysqld.sock"}, charset: utf8, collation: utf8_unicode_ci (if you set the charset manually, make sure you have a matching collation)
I found plenty of stackoverflow questions addressing this problem with the following advice:
Verify that user and password are correct (I'm running w/ no password for root on my dev box)
Verify that the socket is correct - I can cat the socket, so I assume it's correct
Verify that the user can create a DB (As you can see root can connect and create a this DB no problem)
simon#/Users/simon/source/rails/blog/config: mysql -uroot -hlocalhost
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.1.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database blog_development;
Query OK, 1 row affected (0.00 sec)
to ensure that this wasn't a charset issue I also tried:
mysql> create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci;
Query OK, 1 row affected (0.00 sec)
Note: here is my database.yaml:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blog_development
pool: 5
username: root
password:
socket: /opt/local/var/run/mysql5/mysqld.sock
# host: localhost
Note that I tried switching socket to localhost with no effect.
Any idea on what might be going on here?
Thanks for all the help guys. Looks like the problem had to do with my install of the mysql gem under OSX.
#tim after I proceeded to the next step and got up and going I got an error on the console, so I did a bit of searching and found this helpful thread.
After I uninstalled my ruby gems gem uninstall mysql I installed the proper mysql gems using this command (from the thread):
export ARCHFLAGS="-arch i386 -arch x86_64" ; gem install --no-rdoc --no-ri mysql -- --with-mysql-dir=/opt/local/lib/mysql5 --with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config
After executing this one I was able to successfully run rake db:create and proceed.
Thanks!!
It could be a number of things.
Is your database set for utf8 character set?
Is the path to the socket correct, since it varies from OS.
Have you reinstalled the mysql gem? sudo gem install mysql
It might be MySQL, you might want to downgrade the version to 5.0
Other than that, I'm not sure.
You should post here your database.yml
To make your test better, i would try to create a database UTF-8 to see if your database supports utf-8
create database foobar CHARACTER SET utf8 COLLATE utf8_general_ci
Does the blog_development database already exist?
If so, you can just continue to the next step.
Try running ruby script/server in the blog/ directory.
If it doesn't error out, then you should be able to navigate to localhost:3000 and continue the tutorial from here http://guides.rubyonrails.org/getting_started.html#hello-rails.
Leave me a comment if ruby script/server errors out.
I just had the same issue : it was an old mysql gem which was not up to date. Re-installing the mysql gem did the trick.
Re-install mysql-server and mysql-client using this command:
sudo apt-get install mysql-server mysql-client
and then some libraries you need to install to make MySQL available to ruby:
sudo apt-get install libmysql-ruby
This all solved my problem. Try it !!! :)

Resources