-bash: pg_dump: command not found - ruby-on-rails

I'm trying to pull a production database locally for my rails app. My local postgres version was too low so I needed to update to Postgresql 9.6.5 from 9.4.1.
I installed Postgres 9.6.6 via Homebrew as such:
brew install postgresql#9.6
Then ran:
brew services start postgresql#9.6
However, when I try to do pg_dump I get -bash: pg_dump: command not found.
I also tried updating my path as such:
export PATH="/usr/local/Cellar/postgresql\#9.6/9.6.6/bin:$PATH"
Any idea what I need to do to get pg_dump to work?

Adding this to my ~/.bash_profile did the trick:
export PATH=/usr/local/Cellar/postgresql\#9.6/9.6.6/bin:$PATH

For those who have this issue on Mac even though Postgres path is correctly added on your .bash_profile I found that running pg_dump directly from the Terminal does the job.
Just open your Terminal and type:
pg_dump -U username your_database > db_dump.bak
I tried the ff approach and I got the same error:
sudo su - postgres
pg_dump -U username your_database > db_dump.bak
bash: pg_dump: command not found

If you installed PostgreSQL by pgadmin4 in Mac, you could find the pg tool in
/Library/PostgreSQL/{version}/bin/

Couldn't get path to work in Cygwin but could browse to my pg bin directory (C:\Program Files\PostgreSQL\9.6\bin) and run it directly from there.

For windows what worked for me is to add the Postgres to global vars from env, not ~/.bash_profile,
Go to Edit the system environment variables
Then Environement variables
Then Path then add the location of bin of postgres in my case C:\Program Files\PostgreSQL\14\bin\
Click ok on each open window
Close and reopen your visual code
Run your command
See Setting Windows PATH for Postgres tools for more details

Related

Unison command not found

Recently my Unison application has stopped working on my Mac terminal saying the command cannot be found. Not sure if this was the reason but I recently reinstalled brew (which is how it was installed), which may have broken it.
Here is the error output:
mac:~ Imran$ unison sites
-bash: unison: command not found
I can see that if I locate unison unison is found in the following directories:
> /Users/Imran/unison.log /Users/Imran/unison.tar.gz
> /usr/local/Cellar/unison /usr/local/Cellar/unison/2.40.102
> /usr/local/Cellar/unison/2.40.102/COPYING
> /usr/local/Cellar/unison/2.40.102/INSTALL_RECEIPT.json
> /usr/local/Cellar/unison/2.40.102/NEWS
> /usr/local/Cellar/unison/2.40.102/README
> /usr/local/Cellar/unison/2.40.102/TODO.txt
> /usr/local/Cellar/unison/2.40.102/bin
> /usr/local/Cellar/unison/2.40.102/bin/unison
> /usr/local/Library/Formula/unison.rb
> /usr/local/Library/LinkedKegs/unison /usr/local/bin/unison
> /usr/local/opt/unison /usr/share/zsh/5.3/functions/_unison
Can someone help me get this working again? I suspect its something to do with the link to the app being broken.
If you've done a reinstall of a brew cask, you may need to manually link it again if it can't be found on your path
brew link unison
I had the same issue when trying to use Unison from Windows to Mac (where it has been installed with Brew).
You can give an explicit path for the unison executable on the server by using the command-line option
-servercmd /full/path/name/of/unison
or adding
servercmd=/full/path/name/of/unison
to your profile
Source
According to the documentation the error message bash: unison: command not found means:
Make sure Unison is installed on the host you are trying to connect to.
For anyone running into this error on macOS:
If you're using zsh (or running a newer macOS version where zsh is the default) you have to modify or create ~/.zshenv.
Add your path to the shell environment by adding one of the following snippets to ~/.zshenv.
For ARM64 (M1 Macs):
eval $(/opt/homebrew/bin/brew shellenv)
or on x86 (Intel Macs)
eval $(/usr/local/bin/brew shellenv)
or you simply source your zshrc:
source ~/.zshrc
Your user's PATH variable does not include a link to the unison binary.
Add /usr/local/Cellar/unison/2.40.102/bin/unison to your .bashrc,
$ vi ~/.bashrc
add the following,
export PATH=$PATH:/usr/local/Cellar/unison/2.40.102/bin/unison
save and quit, and then re-read your users bashrc file,
exec bash
Now try which unison again, or actually just try running it with,
$ unison

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.

How to specify which postgres copy for rails to use

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.

pg_dump server version mismatch [duplicate]

This question already has answers here:
How to fix pg_dump version mismatch errors?
(6 answers)
Closed 8 years ago.
I'm getting the following error whenever I run rake tasks on my Rails app's database.
pg_dump: server version: 9.2.4; pg_dump version: 9.1.5
pg_dump: aborting because of server version mismatch
I've Googled around, and found some suggestions about updating the postgres brew package, but this did not work.
Why is the incorrect pg_dump version being used, and how can I fix this? Or what search terms should I be looking for to find a solution?
EDIT
Info on my pg_dump configuration:
$ pg_dump --version
> pg_dump (PostgreSQL) 9.1.5
$ which pg_dump
> /usr/bin/pg_dump
$ echo $PATH
> /Users/andrewharvey/.rvm/gems/ruby-1.9.3-p392#mantawatch/bin:/Users/andrewharvey/.rvm/gems/ruby-1.9.3-p392#global/bin:/Users/andrewharvey/.rvm/rubies/ruby-1.9.3-p392/bin:/Users/andrewharvey/.rvm/bin:/usr/local/heroku/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Users/andrewharvey/.rvm/bin
I'm using oh_my_zsh, and path is defined in my .zshrc file. This may be the cause of my problem (and I'm sure this can be cleaned up), unfortunately I don't know enough what I'm doing to debug and edit this. Grateful for pointers (I'm using RVM and postgres installed via Homebrew).
source $ZSH/oh-my-zsh.sh
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"
alias pg='pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log'
export
PATH=$PATH:/usr/local/bin:/Users/andrewharvey/.rvm/gems/ruby-2.0.0-p0/bin:/Users/andrewharvey/.rvm/gems/ruby-2.0.0-p0#global/bin:/Users/andrewharvey/.rvm/rubies/ruby-2.0.0-p0/bin:/Users/andrewharvey/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
### Added by the Heroku Toolbelt
export PATH="/usr/local/heroku/bin:$PATH"
For mac users
put to the top of .profile file.
export PATH="/Applications/Postgres.app/Contents/MacOS/bin:$PATH"
then run
. ~/.profile
What does the output of the following show? It looks like the pg_dump version you are trying to use is a 9.1 version, whereas the cluster you are connecting to is 9.2. If that's the case, then you'll need to determine the correct path of the 9.2 pg_dump.
pg_dump --version
which pg_dump
echo $PATH
[edited after OP provided more details]
According to this page: https://wiki.postgresql.org/wiki/Installers/Mac_OS_X, homebrew probably installed the tools at /usr/local/bin. However, your current $PATH has /usr/local/bin located after several other paths.
I've tidied up your $PATH setting. so try saving the changes below to your .zshrc file and source'ing it to pick up the changes (source ~/.zshrc).
export PATH=/usr/local/bin:/Users/andrewharvey/.rvm/gems/ruby-2.0.0-p0/bin:/Users/andrewharvey/.rvm/gems/ruby-2.0.0-p0#global/bin:/Users/andrewharvey/.rvm/rubies/ruby-2.0.0-p0/bin:/Users/andrewharvey/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:$PATH
export PATH="/usr/local/heroku/bin:$PATH"

postgres 'psql' command is not recognized in windows environment

It seems like I have it set up, I have my database.yml using postgres, and I can connect the database with pgadmin III, but when I tried using the psql command to test what version, it says that is is not recognized? Any idea what I should be doing?
I'm using windows and using command prompt with Ruby on Rails. Using PostgreSQL 9.2 installed with the EnterpriseDB one-click installer.
Assuming you installed PostgreSQL on Windows with the PostgreSQL "One-click" installer packaged by EnterpriseDB, psql is not added to the PATH automatically. That's partly because adding it to the path could otherwise cause confusion when people have multiple versions of PostgreSQL installed.
You need to specify the full explicit path to psql, eg:
"%PROGRAMFILES%\Postgresql\9.2\bin\psql.exe"
or add psql to the user PATH environment variable, see eg this guide.
Open Command Line
psql --version
if on above line psql version is not displayed, then follow below steps
(For Windows 7 OS)
Mycomputer->Properties->Advanced System Settings->EnvironmentVariables->Path->Edit->
(Donot remove any existing Path)
Add this
;C:\Program Files\PostgreSQL\9.5\bin;C:\Program Files\PostgreSQL\9.5\lib
save it then
Reopen Command Prompt and repeat
psql --version
C:\Users\rumi>psql --version
psql (PostgreSQL) 9.5.0
it is because there should not be space between the ; and your psql path so it should be like *****;C:\Program Files\PostgreSQL\9.2\bin\.
If there is a space after ******; then it will not work. You can actually type %PATH%in your cmd prompt and see how your environment variable and space looks like. I tried both cases for you, the first with space had the same error you mentioned and the second without space worked.
You need to set both the bin AND the lib path in your PATH environment variable separated by semicolons:
%PROGRAMFILES%\Postgresql\9.2\bin;%PROGRAMFILES%\Postgresql\9.2\lib

Resources