10

Somewhere along the line I have ended up with a version mismatch between postgresql-9.4 and psql which is version 9.3 despite the fact that version 9.4 is installed. I think I need to correct the $PATH variable but I don't know where to find this. I've looked in my /etc/.bashrc file and can't see anything that points me in the right direction.

When I do sudo find / -name psql the result is:

/usr/bin/psql
/usr/share/bash-completion/completions/psql
/usr/pgsql-9.4/bin/psql

yum list installed | grep postgres yields the following:

postgresql.x86_64                9.3.9-1.fc21                          @updates 
postgresql-contrib.x86_64        9.3.9-1.fc21                          @updates 
postgresql-devel.x86_64          9.3.9-1.fc21                          @updates 
postgresql-libs.x86_64           9.3.9-1.fc21                          @updates 
postgresql-server.x86_64         9.3.9-1.fc21                          @updates 
postgresql94.x86_64              9.4.5-1PGDG.f21                       @pgdg94  
postgresql94-libs.x86_64         9.4.5-1PGDG.f21                       @pgdg94  
postgresql94-server.x86_64       9.4.5-1PGDG.f21                       @pgdg94 
fatherdamo
  • 165
  • 1
  • 2
  • 13

3 Answers3

8

As Craig Ringer answered - generally you should use:

sudo update-alternatives --config pgsql-psql

Nevertheless, sometimes you can get such message back:

failed to link /usr/bin/psql -> /etc/alternatives/pgsql-psql: /usr/bin/psql exists and it is not a symlink

If so - try:

ln -s /usr/pgsql-[PUT YOUR VERSION HERE(ex. 9.6)]/bin/psql /usr/local/bin/psql
Alexander Gorg
  • 1,049
  • 16
  • 32
  • Last one instruction `ln -s /usr/pgsql-9.6/bin/psql /usr/bin/psql` sends `ln: fallo al crear el enlace simbólico «/usr/bin/psql»: El fichero ya existe` – Fred Noriega Nov 22 '18 at 19:32
  • For not spanish speakers the message above means: "failure to create the symbolic link "/usr/bin/psql": The file already exists". Have you tried deleting the existing link with `rm` and making a new one? If I remember right `ln` doesn't replace link. – Alexander Gorg Nov 24 '18 at 17:51
  • 3
    This is exactly the solution I was looking for on Centos 7, where the base repository installed the psql binary directly into `/usr/bin/psql`, so the [yum install of postgres 10](https://www.postgresql.org/download/linux/redhat/) threw the "failed to link" error on that (and 10 other binaries) – Randall Apr 09 '19 at 20:19
3

Use the alternatives mechanism. On Fedora:

sudo update-alternatives --config pgsql-psql
Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
  • After using the advice of Rodrigo Hjort when I use your suggestion the command reads correct and there is only 1 option: `/usr/pgsql-9.4/bin/psql` . But the response on running `psql` is to launch into version 9.3! – fatherdamo Dec 03 '15 at 22:29
  • 1
    @fatherdamo Ah. The issue is that the 9.3 packages don't use alternatives, they're the stock Fedora ones. Do you need them still? – Craig Ringer Dec 03 '15 at 23:20
  • No, I think they were installed when I was trying to get postgresql working. I removed `postgresql.x86_64` which also removed `p*sql-contrib + devel + server` and to be complete removed `postgresql-libs.x86_64`. The `psql` command then failed to launch at all. `update-alternatives --config` reads as I think it should but there is no listing for `/usr/bin/psql`. So I removed `postgresql94` and reinstalled, some of the 9.3 packages returned and the `psql` command launches into 9.3! Weird. – fatherdamo Dec 04 '15 at 10:54
  • I recalled that immediately after removing the 9.3 packages `psql` did not run and the OS suggesting installing `postgresql` this was to enable `psql`. This put 9.3 back on the system. So, I removed 9.3 again and then followed the command `update-alternatives` as below. This has now got everything back on track and `psql` launches into the correct version. – fatherdamo Dec 06 '15 at 18:13
3

You're apparently using CentOS or RHEL.

In this case, you might: 1) invoke psql with the full path or 2) replace the current psql in the alternatives by executing the following command as root:

update-alternatives --install /usr/bin/psql pgsql-psql /usr/pgsql-9.4/bin/psql 1

A tip: use updatedb and locate instead of find whenever looking for a file or directory. :D

Rodrigo Hjort
  • 301
  • 3
  • 5
  • Thanks for this response, yes I'm on Fedora 21. It looks like it should work but when I type `psql` at the command line I still launch in psql version 9.3. I guess that `/usr/bin/psql` is a symbolic link in my bin file named `pgsql-psql` that should execute `/usr/pgsql-9.4/bin/psql` ? The latter command launches correctly to psql version 9.4, `alternatives` seems correctly named, why would 'psql` not work? – fatherdamo Dec 03 '15 at 22:15
  • 1
    Try running `which psql` in order to find out where `psql` points to. If it is a symbolic link, follow it until you discover the final binary being invoked. – Rodrigo Hjort Dec 24 '15 at 02:29