252

I just installed posgresql with homebrew and when I go on to type the command

psql

I get the following error:

dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.2.dylib
Referenced from: /usr/local/bin/psql
Reason: image not found
[1]    69711 trace trap  psql

Does anyone have any idea about what's wrong?

BryanH
  • 5,826
  • 3
  • 34
  • 47
hbejgel
  • 4,674
  • 2
  • 17
  • 27
  • 1
    Looks like it linked to readline, but it's not present. Did you uninstall readline afterwards? How *exactly* did you install PostgreSQL with homebrew? (Steps, commandS). – Craig Ringer Feb 01 '14 at 04:56
  • 1
    Likely solved by https://stackoverflow.com/questions/42356309/mac-psql-readline-library-not-loaded/42377043 which has a more succinct answer than most of the below (you probably don't need to reinstall postgres!) – thclark Dec 20 '18 at 10:57

22 Answers22

388

I was getting the exact same error, but the above answers didn't work for me. I had to reinstall postgresql.

brew reinstall postgresql

dafunkeemonkee
  • 4,085
  • 1
  • 16
  • 5
  • 5
    This one did it for me on El Capitan – Sebastian Sastre Oct 20 '16 at 13:18
  • This was the only one that worked, I tried all the other answers in this page. – Ian Vaughan Nov 09 '16 at 10:02
  • It seems this problem occurred after I installed RVM / Ruby. – David Nov 28 '16 at 03:00
  • 6
    My issue seems to have been related to python installation using brew – Guy Nov 28 '16 at 08:14
  • 7
    Be careful doing this if you aren't already on the latest version of Postgres. You'll need to reinstall the old Postgres alongside the new one and then do a `pg_upgrade` – dazonic Jan 02 '17 at 02:49
  • 9
    This is very heavy handed if you don't want to lose your local databases. I had luck with 'brew switch readline 6.3.8' – Christopher Johnson Jun 15 '17 at 20:17
  • 1
    BE VERY CAREFUL - reinstall removes the previous version of postgres and if you haven't previously backed up your database for the upgrade process, then you're going to have to install the previous version alongside the new version just to perform the data upgrade. See here: https://www.postgresql.org/docs/9.6/static/upgrading.html Better to install the new version rather than reinstall it – drew Jun 16 '17 at 07:34
  • 4
    If you think re-installing psql is a radical step for what seems like a small bug, you're not alone. This answer: https://stackoverflow.com/a/41476592/839876 seems to address the real problem that most of these answers work around. – frandroid Sep 19 '17 at 20:45
  • That worked for me aswell. So brew install doesn't work, but reinstall does.. Wierd. – Martin Sep 08 '18 at 08:27
  • Worked for me, 2019-01-15, when getting a similar message when trying to use the Heroku pg psql cli. – anonymous coward Jan 16 '19 at 01:29
  • 2
    Can do this for a specific version too ex: `brew reinstall postgresql@9.5` – SomeSchmo Feb 20 '19 at 18:44
  • 1
    Wow, this worked for me to. I never run DB's locally for anything other than development purposes, and as such, not critical to keep all of my stuff. So it was a simple fix to do this. I highly recommend listening to the warnings in this thread though. Edit: After double checking, I lost no databases or saved info, but still, read the warnings... – the chad Mar 08 '19 at 20:09
  • Work for me on Mojave (and another program, `tig` not postgresql) – Akhorus Apr 12 '19 at 12:53
  • 1
    Just upgrading postgres to the latest version that uses the updated readline fixed this for me: `brew upgrade postgres` – flurdy Jun 10 '19 at 13:03
  • this IS very heavy handed and should possibly NOT be the top answer – Andrew Jun 17 '19 at 19:41
135

The key problem is that your postgresql was trying to find the libreadline.6.2.dylib but your readline is version 7.0, so only 7's lib is linked.

I don't think upgrading postgresql is a good idea, it's not easy and may cause a lot of problem, especially when you already have database data.

What I do, is to switch the version of readline. When you check brew info readline, you will find different versions, mine is 6.3.8, so I switch to that version by executing:

$ brew switch readline 6.3.8

It will change to 6.3.8 and create the link:

Cleaning /usr/local/Cellar/readline/6.3.8
Cleaning /usr/local/Cellar/readline/7.0.1
Opt link created for /usr/local/Cellar/readline/6.3.8

In this case, all the linked lib becomes to 6 version, and command line works:

$ ls /usr/local/opt/readline/lib/
libhistory.6.2.dylib    libhistory.dylib    libreadline.a
libhistory.6.3.dylib    libreadline.6.2.dylib   libreadline.dylib
libhistory.6.dylib  libreadline.6.3.dylib
libhistory.a        libreadline.6.dylib

However, if you have different apps using the different version of readline, I think you should consider upgrading.


Update: 06/02/2019

What if the old version is not installed?

Homebrew doesn't provide a way to install old version formula. And since v2.0.0, it will run brew cleanup as default behaviours every 30 days. Therefore, you may accidentally update brew and delete the old version.

Here are the steps to install old version:

  1. Go to homebrew-core directory:
    $ cd $(brew --repository)/Library/Taps/homebrew/homebrew-core
  1. Check the hash for old Formula:
    $ git log master -- Formula/readline.rb
  1. Find the corresponding version of the hash value
    commit 1dd4221c35716d6bec363ad3425ef93ffe7d9b9f
    Author: BrewTestBot <brew-test-bot@googlegroups.com>
    Date:   Wed Aug 15 21:51:16 2018 +0000

        readline: update 7.0.5 bottle.
  1. Checkout to old formula:
    $ git checkout 1dd4221c35716d6bec363ad3425ef93ffe7d9b9f Formula/readline.rb
  1. Reinstall the formula:
    $ brew reinstall readline
  1. Reset the repo

Following those steps, you can reinstall the old version of formula.

Stephen
  • 3,822
  • 2
  • 25
  • 45
  • 1
    And me - if you have no other dependencies on readline 7 then this is definitely the least disruptive approach. – drew Jun 16 '17 at 07:32
  • this worked for me also, saved me from updating postges – Rich Oct 12 '17 at 12:56
  • Worked for me too...much cleaner solution than updating the whole databse – sromit Apr 17 '18 at 20:00
  • When would one want to switch back to whatever other version of `readline` that was installed? What other downsides are there to doing this? – Kenny Evitt Jun 15 '18 at 00:51
  • I only have one readline installed, and my local checkout of readline only contains 8.0, and the log doesn't have any older version commits, even after fetching everything. Looks like it's impossible to install an older version of readline for me, and upgrading postgres may be the only option – Andy Ray Mar 20 '19 at 19:15
  • Using the steps to install an older version doesn't seem like it'll help for installing versions older than 7.0.5 as that's the oldest version mentioned in any commits touching the *readline* formula. – Kenny Evitt Jun 12 '19 at 19:43
79

If the situation is you have a higher version readline say 7.0. You can do this

cd /usr/local/opt/readline/lib    
ln libreadline.7.0.dylib libreadline.6.2.dylib
anvy zhang
  • 807
  • 6
  • 4
  • 5
    I had to create link named "libreadline.6.dylib" but this was the only suggestion that worked for me. – Mads Hoel Jan 15 '17 at 15:22
  • 1
    This in conjunction with `brew uninstall --force gawk` and then `brew install gawk` was what got me up and running. You might also be able to just `brew reinstall --force gawk` – Csteele5 Mar 14 '17 at 15:58
  • 1
    Good one. I am running postgres 10.6 which expects libreadline.7.dylib but I had just installed `pg_top` which upgraded libreadline to 8.0 and broke psql. I used your trick, except made it a symlink `ln -s libreadline.8.0.dylib libreadline.7.dylib` – Davos Mar 14 '19 at 03:54
  • Excellent, thanks. This worked on MacOS Mojave without having to uninstall and re-install PostgreSQL (which would have been a real pain). – crmpicco Mar 15 '19 at 08:21
46

If you don't have readline installed, first install it:

brew install readline

For me, I was getting this error, even though I had readline installed. Reinstalling readline did the trick:

brew reinstall readline
Troy
  • 21,172
  • 20
  • 74
  • 103
  • 6
    reinstalling readline didn't do it for me, but uninstall/install `bash` did: `brew install --build-from-source bash` – whatbird Sep 24 '14 at 18:49
  • I found that I had to remove all verison of readline first (brew uninstall --force readline). After I reinstalled it, I had to force link it with "brew link --force readline" – Wilhelm Sep 26 '14 at 16:45
  • Reinstalling bash was the fix for me, too, but in my case the "referenced from" line mentioned bash specifically. – mbafford Oct 31 '16 at 16:58
  • After installing MacOS Sierra, I had been encountering this issue. I did both `brew reinstall readline` (to get the Sierra-specific brew) and while at it, `brew reinstall postgresql94`. People said to reinstall bash but I never had it to begin with. So this answer and the chosen answer both worked for me. – risa_risa Dec 26 '16 at 22:28
  • brew reinstall readline worked for me after switching to other versions (which has worked in the past) no longer worked after upgrading to psql9.6 – bkunzi01 Jun 08 '17 at 15:38
32

This worked for me

brew switch readline

This would display the versions you have installed. Pick one out of them. I picked 7.0.5

I then ran

brew switch readline 7.0.5

The readline version got updated, and psql ran smoothly.

henry
  • 4,244
  • 2
  • 26
  • 37
sindhu_sp
  • 749
  • 5
  • 12
  • 2
    Also worked for me. psql was trying to load `libreadline.7.dylib`, but I was using readline 8.0.0. Switching from 8.0.0 to 7.0.5 solved the problem. I expect that creating a symlink (as others have done) would also work, if I wanted to stay on 8.0.0. But having a newer version link to an older version is misleading and could potentially cause other issues. – Dennis Jan 16 '19 at 11:30
  • How did you find this solution? I'm very impressed! – kuzdu Jan 21 '19 at 06:11
  • 1
    I tried the same, but got the following error: readline does not have a version "7.0.5" in the Cellar. I tried brew install 7.0.5 readline, but it shows the following error: No available formula with the name "7.0.5" .@Dennis – Mahesh Mesta Feb 04 '19 at 09:30
23

this worked for me

ln -s /usr/local/opt/readline/lib/libreadline.7.0.dylib /usr/local/opt/readline/lib/libreadline.6.dylib
Kenny Evitt
  • 9,291
  • 5
  • 65
  • 93
Jon
  • 555
  • 5
  • 5
  • This was my problem as well. I did not use brew and was missing the sym link after latest update. Thanks! – Matt Apr 30 '18 at 22:43
  • Nice! My exact error complained about 6.2, my command is slightly different. The error was `dyld: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.2.dylib`, so I used `ln -s /usr/local/opt/readline/lib/libreadline.7.0.dylib /usr/local/opt/readline/lib/libreadline.6.2.dylib`. – Tyler Collier Jun 14 '18 at 23:35
20

The solution is to force the linking of readline again.

brew remove readline
brew install readline
brew link readline --force
Alex Palcuie
  • 4,434
  • 3
  • 22
  • 27
14

It seems to me the problem is about update readline package. postgres tryied to use /usr/local/opt/readline/lib/libreadline.7.dylib in my case. So i just created a symlink to existing version of library /usr/local/opt/readline/lib/libreadline.8.dylib.

worked for me:

ln -s /usr/local/opt/readline/lib/libreadline.8.dylib /usr/local/opt/readline/lib/libreadline.7.dylib
Eugeny Maksimov
  • 181
  • 1
  • 7
  • This worked for me. I'm on psql version 9.6.3 and readline 8. – lacostenycoder Aug 22 '19 at 18:08
  • Admittedly this is a hacky way to go about fixing the issue, but I was running into this problem after some unrelated upgrades pushed my readline version from 7 to 8, and a full Postgres reinstall wasn't something I wanted to tackle right now. This is a small footprint solution and the risk of something impactful changing in readline seems small, so I'm going with this for now and moving on. Will report back if it causes any issues down the line. – Scott Roth Jan 06 '20 at 17:56
  • Thanks for comment, I agree this issue. I had a lot of problems after updating to macos catalina, so I didn't want to digg into this issue and used the hack. – Eugeny Maksimov Jan 08 '20 at 16:01
8

None of those solutions worked for me; I had to run :

brew upgrade bash

from : https://github.com/Homebrew/homebrew-core/issues/5799

Anthony Dahanne
  • 4,823
  • 2
  • 40
  • 39
  • in my el capitan after upgrade to 10.11.6 got this error too each time open terminal app, already solved by upgrading bash – azul Nov 11 '16 at 05:47
6

The answer above didn't work for me so I wanted to post what did eventually work. Based on a thread I found here, I had to uninstall readline and the install it again but from source.

brew uninstall readline
brew install --build-from-source readline

After that the psql error went away.

Mark Pruce
  • 945
  • 10
  • 25
5

I had this issue, and the key for me was to reinstall both readline and postgres. Once postgres was reinstalled, the issue was resolved.

Micah Bales
  • 181
  • 8
  • 16
5

Another way to fix psql is update postgresql just running brew upgrade postgresql since the newest version will use readline 7 version.

zegomesjf
  • 51
  • 1
  • 4
4

I had this problem when I updated readline to version 7 by accident. I uninstalled readline and brew remind me 6.3.8 is still installed. It seems that version 7 is not working with PSQL at the moment.

Jake
  • 4,829
  • 2
  • 33
  • 44
Yuanqi Cao
  • 101
  • 1
  • 5
4

I tried all these terminal commands and nothing worked so decided to fix it manually..

Open finder and hit command+shift+g

Go to this folder /usr/local/opt/readline/lib/

you should see multiple versions of these files

libreadline.6.dylib
libreadline.7.0.dylib

rename any of them to whichever library is not loaded

this is: /usr/local/opt/readline/lib/libreadline.6.2.dylib
mine was: /usr/local/opt/readline/lib/libreadline.6.dylib
Prince Abalogu
  • 385
  • 2
  • 4
  • 17
3

I tried all the previews answers, and nothing worked. The only thing that really worked for me was:

brew link postgresql

finx
  • 1,293
  • 9
  • 9
3

The following commands worked for me:

brew reinstall postgresql
brew postgresql-upgrade-database
nilansh bansal
  • 1,404
  • 1
  • 12
  • 23
2

This had helped me: uninstall all readline and postgresql

export CPPFLAGS=-I/usr/local/opt/readline/include export LDFLAGS=-L/usr/local/opt/readline/lib

and install posqtgresql

ph4n70m
  • 300
  • 2
  • 6
1

I found this solution helpful

brew upgrade bash

https://github.com/Homebrew/homebrew-core/issues/5799

Tornike
  • 1,245
  • 12
  • 35
0

You could try brew doctor and see what it says.

Possibly all you need is brew link --overwrite bash.

For me, I just upgraded brew since OS X EL Capitan had some security upgrade that affetcs my /usr folder. During the brew upgrade, the bash link is broken, and this is what brew doctor tells me.

So I just run brew link --overwrite bash and everything's fine.

Hustlion
  • 2,203
  • 1
  • 19
  • 22
0

You might want to check if you have anything else symlinked to postgresql. I had previously linked to the postgres93 package, which, as of May '17, is unsupported and has been removed. Removing that link and then relinking worked for me:

brew unlink postgresql93
brew link postgresql
sp89
  • 408
  • 4
  • 9
0

In my case the correct answer did not fix the problem.

The problem started after running brew doctor and adding export PATH="/usr/local/bin:$PATH" to my ~/.zshrc file.

Removing

export PATH="/usr/local/bin:$PATH" 

from ~/.zshrc got it solved.

user9869932
  • 6,571
  • 3
  • 55
  • 49
0

For some unknown reason, when this error popped up for the next version of Readline, my RVM seemed unable to locate the right one no matter how I symlinked it, throwing:

Library not loaded: /usr/local/opt/readline/lib/libreadline.7.dylib (LoadError)

I ended up adding the gem rb-readline to my Gemfile and doing another bundle install and it started working.

I... just don't know.

Erik Trautman
  • 5,883
  • 2
  • 27
  • 35