How to specify which postgres copy for rails to use - ruby-on-rails

How do I specify which version of postgres rails use? When I run puma and go to localhost:3000 I get a an error
PG::ConnectionBad
fe_sendauth: no password supplied
I think the copy being used may be the manual install 9.3, as when I run:
/usr/local/Cellar/postgresql/9.4.1/bin/pg_ctl -D /usr/local/var/postgres start
I get the error:
stgres start
server starting
LOG: skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.3, which is not compatible with this version 9.4.1.
I found three copies of pg_hba.conf on my system:
/Library/PostgreSQL/9.3/data/pg_hba.conf
/Users/lasernite/Library/Application Support/Postgres/var-9.4/pg_hba.conf
/usr/local/var/postgres/pg_hba.conf
The first one I believe was from a manual install at some point. The second one is probably just some supporting copy/ignorable, and the third one is a homebrew install.
How can I make rails use the homebrew postgres install, even if it means wiping the local database? Which is fine so long as the production on heroku is instact.
I've been stuck for several days on reconfiguring my development environment from sqlite to postgres, which is very problematic as I have a production db and site live now, which is forcing me to do some code pushes without being able to verify functionality locally, but for the most part has totally crippled my ability to do any development.
Please help me!

It looks like the problem is that you're starting 9.4.1 when the database was created by 9.3.x.
The version of posgres that Rails uses should be whichever one is running. So if you want to start postgres in version 9.3.x, then you should start that version. But you have to specify the correct path for that version.
What output do you get for these?
> initdb --version
> pg_ctl --version
> postgres --version
> psql --version
It should all be the same. If it says 9.4.x and you want to use that version, then you can re-init the database like so: initdb -D /usr/local/var/postgres-4.1 and then you can start postgres postgres -D /usr/local/var/postgres-4.1. Doing it this way, you'll probably lose your local database since it sounds like that data was created by 9.3.x.
Alternatively, if those output 9.3.x, then you should just be able to use the commands without the full path to the binary: postgres -D /usr/local/var/postgres.
If you're using 9.4.x and you want to keep using 9.3.x, then try which postgres. It's probably pointing to /usr/local/bin. Then make sure that that is just a link to the homebrew version. ls -la /usr/local/bin | grep "postgres ->".
If you're using the homebrew version, you can do brew switch postgres 9.3.x to tell homebrew to use that version. Then you should be able to start postgres with postgres -D /var/local/var/postgres.

Related

Cannot createdb in postgresql

I'm trying to start a Ruby on Rails app with PostgreSQL as the database because once this site is finished I'll be promoting it to another blog that has 259K followers and I just want to anticipate if the user base for my new website gets big.
Anyway I followed this tutorial: https://gorails.com/setup/osx/10.11-el-capitan at first. But when I made my app with PostgreSQL the database would fail to create.
So I stepped back, tried uninstalling and reinstalling postgres, I updated my homebrew, and now I'm following this new tutorial:
https://www.codefellows.org/blog/three-battle-tested-ways-to-install-postgresql#macosx
I'm at the step: Create a Default db Based on Your Username.
I run the command and get this:
Mayas-MacBook-Air:~ mayaah$ which psql
/usr/local/bin/psql
Mayas-MacBook-Air:~ mayaah$ createdb `whoami`
createdb: could not connect to database template1: 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 don't know what to do from here. I've googled this problem extensively and couldn't find an answer. The only possible problem I can think of is my Mac has very little space left, as I saw something about "shared space" which I don't really understand.
Any insight into this, especially with specific step-by-step command line instructions would be super helpful. Thanks!
Edit:
Now I found this tutorial: http://exponential.io/blog/2015/02/21/install-postgresql-on-mac-os-x-via-brew/
Tried to run
postgres -D /usr/local/var/postgres
Got this:
LOG: skipping missing configuration file "/usr/local/var/postgres/postgresql.auto.conf"
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.3, which is not compatible with this version 9.4.5.
I don't know what to do from here either.
At first, you installed PostgreSQL version 9.3, then you uninstalled it and installed version 9.4. But some old files stayed there and PostgreSQL 9.4 is not compatible with them. If you don't need any data of 9.3 version then you can remove them and initialize new settings for 9.4 version:
rm -rf /usr/local/var/postgres
initdb /usr/local/var/postgres -E utf8

Ruby on Rails 4: when type rails server , got error message PG::ConnectionBad

I am new to Ruby on rails. I've created basic demo apps by tutorial learning by examples.
First it working fine, one week later i got error when type rails s,
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 "/tmp/.s.PGSQL.5432"?
I am using PostgreSQL database.
I am on MAC OS X 10.8.5
I have found similar questions on SO, but they are experiencing at some advance stage and thus involves some advance solutions.
when type
which psql
/usr/local/bin/psql
It seems upgrading your OS has changed the permissions on the directory where Postgres keeps its data.
Run
sudo chown -R sabir:staff /usr/local/var/postgres
to make your user account the owner of that directory. Enter your password when it asks for it; you will not see the characters as you enter them.
The homebrew version of Postgres includes the ability to start automatically. To check whether your installation does, run
ls -l ~/Library/LaunchAgents/
You should see a line ending with homebrew.mxcl.postgresql.plist. If you do not see this line, run
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
in order to have it start automatically.
Finally, run
launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
to make sure Postgres is active now. Confirm by typing
psql -l
When you install postgres using homebrew, than maybe you just started it manually in foreground. So after closing the terminal window postgres was not running anymore.
I'd suggest to do the following steps:
Check if postgres is running by grepping the process list: ps aux | grep postgres
if it's running try to sudo kill -9 {POSTGRES_PID}
if not running, use brew info postgres to see how to start it manually (or on startup etc.)
start postgres with the result from homebrew - in my case postgres -D /usr/local/var/postgres
I hope, that will help you out.
If you installed postgres on another basis, I can change my answer accordingly.

Rails - OS Mavericks and postgresql

Background - a few months ago, I purchased a new Macbook to replace an iMac. When I restored my data from my old iMac to the new Macbook, I accidentally used davidburton as the user when on the iMac I was burtondav.
I was then not able to run postgreSQL or Rails.
I used this symlink command to fix it:
ln -s /Users/davidburton /Users/burtondav
But, yesterday I upgraded to OS X Mavericks. Now I can't start postgreSQL or use Rails. Even after re-entering the symlink.
Is there any way to fix it???
If I have to re-install postgreSQL:
My postgreSQL files are in usr/local/var/postgres
I'm pretty sure I used homebrew to install it.
How can I re-install postgreSQL without loosing my data?
I was going to dump the whole database and reload after I reinstall postgreSQL. But I can't start postgreSQL in order to dump the data.
This is what I get:
$ postgres -D /usr/local/var/postgres
2013-10-24 15:30:07 GMT < > %FATAL: data directory "/usr/local/var/postgres" has wrong ownership
2013-10-24 15:30:07 GMT < > %HINT: The server must be started by the user that owns the data directory.
Should I change the ownership from burtondav to davidburton?
Thanks for your help!!
You should change the owner of the postgres data directory:
chown davidburton /user/local/var/postgres
# or
sudo chmod ugo=rwX /user/local/var/postgres # You might want to change the permissions as your needs
(You might need to use sudo)
You didn't post the error when you try to start rails/postgres, maybe this question can help you: ERROR: Failed to build gem native extension on Mavericks

ps auxwww | grep psql - postgres not running with homebrew

I have installed Postgres 9.2.3 just now via Homebrew, initiated a db, and inputed the start command. It says "server starting", but whenever I try to do anything, it tells me:
psql: could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/tmp/.s.PGSQL.5432"?
Here are the things I have already done to try and fix this:
1) Fixed the path in my bash profile by adding this text: export PATH=/usr/local/bin:$PATH
2) uninstalled & reinstalled the pg gem
3) used the gem pristine function
4) tried uninstalling and reinstalling home-brew
5) removed the old version of postgres that came with the Mac
6) sudo chmod -R 777 /var/pgsql_socket/
Still, nothing I do works. I have even tried using the Postgres official installer and the Postgres App from Heroku. Nothing works.
It seems like I can't get postgres to run properly because when I run: ps auxw | grep postgres
I just get:
[MyName] 66744 0.0 0.0 2432768 596 s000 R+ 8:47PM 0:00.00 grep postgres
nothing else shows up, which suggests that nothing else is running.
I noticed that my username for my Mac is Zephyr, but the username postgres is using is my actual name. Could that be the issue?
Which I do which psql, I get the correct path of: /usr/local/bin/psql
I have been trying to get this to work for hours and nothing seems to resolve this connection issue. Any help would be appreciated!
I believe you need to setup postgresql manually which in some case is well hidden from user.
You can try these specific steps and see if it works(At least, worked in my Lion). Bear in mind, It is my local setup. I installed Postgresql from binary.
create a new folder named data in postgres folder(mine is at /opt/local/lib/postgresql90/data). Make sure your chmod that folder. data is just simple to remember.
run initdb to initialize postgres data. Mine is
/opt/local/lib/postgresql90/bin/initdb -D
/opt/local/lib/postgresql90/data.
Start server:
/opt/local/lib/postgresql90/bin/pg_ctl -D
/opt/local/lib/postgresql90/data start
Tips: Put these paths into env variables.
Hope it helps :)

Can not connect to local PostgreSQL

I've managed to bork my local development environment.
All my local Rails apps are now giving the error:
PGError
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've no idea what's caused this.
While searching for a solution I've updated all bundled gems, updated system gems, updated MacPorts. No joy.
Others have reported this issue when upgrading from OSX Leopard to Lion, due to confusion over which version of Postgres should be used (i.e., OSX version or MacPorts version). I've been running Lion for several months, so it seems strange that this should happen now.
I'm reluctant to mess around too much without first understanding what the problem is. How can I debug this methodically?
How can I determine how many versions of PostgreSQL are on my system, which one is being accessed, and where it is located? How do I fix this if the wrong PostgreSQL is being used?
Sorry for the noob questions. I'm still learning how this works! Thanks for any pointers.
EDIT
Some updates based on suggestions and comments below.
I tried to run pg_lsclusters which returned a command not found error.
I then tried to local my pg_hba.conf file and found these three sample files:
/opt/local/share/postgresql84/pg_hba.conf.sample
/opt/local/var/macports/software/postgresql84/8.4.7_0/opt/local/share/postgresql84/pg_hba.conf.sample
/usr/share/postgresql/pg_hba.conf.sample
So I assume 3 versions of PSQL are installed? Macports, OSX default and ???.
I then did a search for the launchctl startup script ps -ef | grep postgres which returned
0 56 1 0 11:41AM ?? 0:00.02 /opt/local/bin/daemondo --label=postgresql84-server --start-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper start ; --stop-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper stop ; --restart-cmd /opt/local/etc/LaunchDaemons/org.macports.postgresql84-server/postgresql84-server.wrapper restart ; --pid=none
500 372 1 0 11:42AM ?? 0:00.17 /opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/defaultdb
500 766 372 0 11:43AM ?? 0:00.37 postgres: writer process
500 767 372 0 11:43AM ?? 0:00.24 postgres: wal writer process
500 768 372 0 11:43AM ?? 0:00.16 postgres: autovacuum launcher process
500 769 372 0 11:43AM ?? 0:00.08 postgres: stats collector process
501 4497 1016 0 12:36PM ttys000 0:00.00 grep postgres
I've posted the contents of postgresql84-server.wrapper at http://pastebin.com/Gj5TpP62.
I tried to run port load postgresql184-server but received an error Error: Port postgresql184-server not found.
I'm still very confused how to fix this, and appreciate any "for dummies" pointers.
Thanks!
EDIT2
This issue began after I had some problems with daemondo. My local Rails apps were crashing with an application error along the lines of "daemondo gem can not be found". I then went through a series of bundle updates, gem updates, port updates and brew updates to try and find the issue.
Could this error be an issue with daemondo?
This really looks like a file permissions error. Unix domain sockets are files and have user permissions just like any other. It looks as though the OSX user attempting to access the database does not have file permissions to access the socket file. To confirm this I've done some tests on Ubuntu and psql to try to generate the same error (included below).
You need to check the permissions on the socket file and its directories /var and /var/pgsql_socket. Your Rails app (OSX user) must have execute (x) permissions on these directories (preferably grant everyone permissions) and the socket should have full permissions (wrx). You can use ls -lAd <file> to check these, and if any of them are a symlink you need to check the file or dir the link points to.
You can change the permissions on the dir for youself, but the socket is configured by postgres in postgresql.conf. This can be found in the same directory as pg_hba.conf (You'll have to figure out which one). Once you've set the permissions you will need to restart postgresql.
# postgresql.conf should contain...
unix_socket_directory = '/var/run/postgresql' # dont worry if yours is different
#unix_socket_group = '' # default is fine here
#unix_socket_permissions = 0777 # check this one and uncomment if necessary.
EDIT:
I've done a quick search on google which you may wish to look into to see if it is relavent.
This might well result in any attempt to find your config file failing.
http://www.postgresqlformac.com/server/howto_edit_postgresql_confi.html
Error messages:
User not found in pg_hba.conf
psql: FATAL: no pg_hba.conf entry for host "[local]", user "couling", database "main", SSL off
User failed password auth:
psql: FATAL: password authentication failed for user "couling"
Missing unix socket file:
psql: 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"?
Unix socket exists, but server not listening to it.
psql: could not connect to server: Connection refused
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
Bad file permissions on unix socket file:
psql: could not connect to server: Permission denied
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
My gut feeling is that this is (again) a mac/OSX-thing: the front end and the back end assume a different location for the unix-domain socket (which functions as a rendezvous point).
Checklist:
Is postgres running: ps aux | grep postgres | grep -v grep should do the trick
Where is the socket located: find / -name .s.PGSQL.5432 -ls (the socket used to be in /tmp; you could start looking there)
even if you locate the (unix-domain) socket, the client could use a different location. (this happens if you mix distributions, or of you have a distribution installed someplace and have another (eg from source) installation elsewhere), with client and server using different rendez-vous addresses.
If postgres is running, and the socket actually exists, you could use:
psql -h /the/directory/where/the/socket/was/found mydbname
(which attempts to connect to the unix-domain socket)
; you should now get the psql prompt: try \d and then \q to quit. You could also
try:
psql -h localhost mydbname.
(which attempts to connect to localhost (127.0.0.1)
If these attempts fail because of insufficient authorisation, you could alter pg_hba.conf (and SIGHUP or restart) In this case: also check the logs.
A similar question: Can't get Postgres started
Note: If you can get to the psql prompt, the quick fix to this problem is just to change your config/database.yml, add:
host: localhost
or you could try adding:
host: /the/directory/where/the/socket/was/found
In my case, host: /tmp
Try uninstalling the pg gem (gem uninstall pg) then reinstalling -- if you use bundler, then bundle install, else gem install pg. Also, make sure path picks up the right version: Lion has a version of posgresql (prior versions didn't) and it may be in the path before your locally installed version (e.g. MacPorts, homebrew).
In my case: homebrew install of postgresql, updated postgresql, rails, etc. and then got this error. Uninstalling and reinstalling the pg gem did it for me.
The location of the socket file is baked into the gem at compile time. Thus you need to rebuild your pg gem.
gem pristine pg
# or
bundle exec gem pristine pg
This should resolve that particular issue.
If you're getting a similar 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"?
This might do the trick (it did for me):
initdb /usr/local/var/postgres -E utf8
The directory specified should be different if you're not using OSX/Brew.
Note: This is not the exact error message seen above, but this thread is the first result for that error message.
what resolved this error for me was deleting a file called postmaster.pid in the postgres directory. please see my question/answer using the following link for step by step instructions. my issue was not related to file permissions:
psql: could not connect to server: No such file or directory (Mac OS X)
the people answering this question dropped a lot of game though, thanks for that! i upvoted all i could
This is how I solved that error message, based partly on wildplasser's answer.
find / -name .s.PGSQL.5432 -ls 2> /dev/null
=> ... /tmp/.s.PGSQL.5432
So, there's my socket or whatever, but the client looks for it at:
/var/run/postgresql/.s.PGSQL.5432
So quite simply make a symbolic link to the /tmp/.s.PGSQL.5432:
sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
Hope this helps to anyone. The seems kind of wrong, but hey, it works!
I started getting this after upgrading to a new postgres - I didn't realize I had hold data files.
First I tried to start the postgres server:
postgres -D /usr/local/var/postgres
which is how I saw this error
FATAL: database files are incompatible with server
DETAIL: The data directory was initialized by PostgreSQL version 9.0, which is not compatible with this version 9.3.5.
So then I found this answer on SO - related to an incompatibility error: https://serverfault.com/questions/342626/how-do-i-upgrade-postgresl-database-incompatibility-error
This is what fixed it
mv /usr/local/var/postgres /usr/local/var/postgres.old
initdb -D /usr/local/var/postgres
Just confirming I had a similar issue on PSQL and Django,
Looked like because my psql server was not shut down correctly and the postmaster.pid file was still present (should be deleted on proper shutdown automatically) in my postgres folder.
Deleted this and all good
I was getting this same error (it turns out it was an error with postmaster.pid. Here's how I got postgres up and running again (credit to Ricardo Burillo for the fix):
$ rm /usr/local/var/postgres/postmaster.pid
$ pg_resetxlog -f /usr/local/var/postgres
I had similar problem when trying to use postgresql with rails. Updating my Gemfile to use new version of gem pg solve this problem for me. (gem pg version 0.16.0 works). In the Gemfile use:
gem 'pg', '0.16.0'
then run the following to update the gem
bundle install --without production
bundle update
bundle install
This happened to me today after my Macbook's battery died. I think this can be caused by improper shutdown. All you have to do in cases such as mine is delete postmaster.pid
Navigate to the folder
cd /usr/local/var/postgres
Check to see if postmaster.pid is present
ls
Remove postmaster.pid
rm postmaster.pid
I read many topics about this error and the solution to me was to simply restart the postgres with:
sudo service postgresql restart
Which is not mentioned here.
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"?
I searching the solution for a while. So, this one fixed the issue for me as well (reinit db):
rm -r /usr/local/var/postgres
initdb /usr/local/var/postgres -E utf8
pg_ctl -D /usr/local/var/postgres -l logfile start
I use OS X 10.11.3 with brew.
In my case none of previous solutions was good. Instead of using socket, you can use TCP host + port number in Rails config file. So in database.yml file just add two lines like here:
...
adapter: postgresql
encoding: unicode
pool: 5
host: localhost
port: 5432
This solved my problem :)
Before I used this fix:
sudo mkdir /var/run/postgresql
sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
But after each reboot /tmp/.s.PGSQL.5432 was deleted and I had to repeat these commands. Solution works, but it is horrible, so better just modify Rails database config file :)
Got this error when I was setting up Posgtres with Django, I'm using Back Track and it comes with Postgres installed. I assume the settings are the issue. I fixed it by removing it completely then reinstalling like so.
sudo apt-get remove postgresql
sudo apt-get purge postgresql
Now run:
apt-get --purge remove postgresql\*
to remove everything PostgreSQL from your system. Just purging the postgres package isn't enough since it's just an empty meta-package.
Once all PostgreSQL packages have been removed, run:
rm -r /etc/postgresql/
rm -r /etc/postgresql-common/
rm -r /var/lib/postgresql/
userdel -r postgres
groupdel postgres
You should now be able to:
apt-get install postgresql
MacOSX here. I had the same problem after upgrading my postresql install from a pre-9.1 to 9.1.2 using homebrew. (By the way, remember to dump databases before your upgrade with pg_dump, pre-9.1 databases are incompatible.) Same problem, same error messages.
Uninstalling the pg gem did the trick for me. I actually had to do quite a bit of dancing to discover the issue. First I did a global gem uninstall, clearing the deck of all the old gems (there were a few). Then I removed pg from my Gemfile, rebundled, restored the pg reference, and rebounded once more.
After that, it worked like a charm.
Hello world :)The best but strange way for me was to do next things.
1) Download postgres93.app or other version. Add this app into /Applications/ folder.
2) Add a row (command) into the file .bash_profile (which is in my home directory):
export PATH=/Applications/Postgres93.app/Contents/MacOS/bin/:$PATH
It's a PATH to psql from Postgres93.app. The row (command) runs every time console is started.
3) Launch Postgres93.app from /Applications/ folder. It starts a local server (port is "5432" and host is "localhost").
4) After all of this manipulations I was glad to run $ createuser -SRDP user_name and other commands and to see that it worked! Postgres93.app can be made to run every time your system starts.
5) Also if you wanna see your databases graphically you should install PG Commander.app. It's good way to see your postgres DB as pretty data-tables
Of, course, it's helpful only for local server. I will be glad if this instructions help others who has faced with this problem.
I had this problem plaguing me, and upon further investigation (running rake db:setup), I saw that rails was trying to connect to a previously used postgres instance - one which was stored in env variables as DATABASE_URL.
The fix: unset DATABASE_URL
source: https://stackoverflow.com/a/17420624/2577622
I tried most of the solutions to this problem but couldn't get any to work.
I ran lsof -P | grep ':5432' | awk '{print $2}' which showed the PID of the process running. However I couldn't kill it with kill -9 <pid>.
When I ran pkill postgresql the process finally stopped. Hope this helps.
gem uninstall pg
On OS X with Homebrew:
gem install pg -- --with-pg-config=/usr/local/bin/pg_config

Resources