253

I just installed PostgreSQL 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

What's wrong?

2
  • 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). Feb 1, 2014 at 4:56
  • 1
    Likely solved by stackoverflow.com/questions/42356309/… which has a more succinct answer than most of the below (you probably don't need to reinstall postgres!)
    – thclark
    Dec 20, 2018 at 10:57

22 Answers 22

390

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

brew reinstall postgresql

15
  • 5
    This one did it for me on El Capitan Oct 20, 2016 at 13:18
  • 6
    My issue seems to have been related to python installation using brew
    – Guy
    Nov 28, 2016 at 8: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 2, 2017 at 2: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' Jun 15, 2017 at 20:17
  • 4
    If you think re-installing psql is a radical step for what seems like a small bug, you're not alone. This answer: stackoverflow.com/a/41476592/839876 seems to address the real problem that most of these answers work around.
    – frandroid
    Sep 19, 2017 at 20:45
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 <[email protected]>
    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.

6
  • 1
    And me - if you have no other dependencies on readline 7 then this is definitely the least disruptive approach.
    – drew
    Jun 16, 2017 at 7:32
  • this worked for me also, saved me from updating postges
    – Rich
    Oct 12, 2017 at 12:56
  • Worked for me too...much cleaner solution than updating the whole databse
    – sromit
    Apr 17, 2018 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? Jun 15, 2018 at 0: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, 2019 at 19:15
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
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, 2017 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, 2017 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, 2019 at 3: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, 2019 at 8: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
5
  • 7
    reinstalling readline didn't do it for me, but uninstall/install bash did: brew install --build-from-source bash
    – whatbird
    Sep 24, 2014 at 18:49
  • 1
    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, 2014 at 16:45
  • 1
    Reinstalling bash was the fix for me, too, but in my case the "referenced from" line mentioned bash specifically.
    – mbafford
    Oct 31, 2016 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, 2016 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 8, 2017 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.

3
  • 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, 2019 at 11:30
  • How did you find this solution? I'm very impressed!
    – kuzdu
    Jan 21, 2019 at 6: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 Feb 4, 2019 at 9: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
2
  • This was my problem as well. I did not use brew and was missing the sym link after latest update. Thanks!
    – Matt
    Apr 30, 2018 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. Jun 14, 2018 at 23:35
20

The solution is to force the linking of readline again.

brew remove readline
brew install readline
brew link readline --force
1
  • relink didn't work for me, but reinstalling postgresql worked. Mar 8, 2017 at 23:09
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
3
  • This worked for me. I'm on psql version 9.6.3 and readline 8. Aug 22, 2019 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 6, 2020 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. Jan 8, 2020 at 16:01
8

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

brew upgrade bash

From: Library not loaded: /usr/local/opt/readline/lib/libreadline.6.dylib #5799

1
  • 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, 2016 at 5:47
6

Troy's answer 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.

0
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.

5

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

4

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

4

I tried all these terminal commands and nothing worked, so I 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
1
  • Works well for me
    – mike_dz
    Apr 4, 2019 at 5:16
3

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

brew link postgresql

3

The following commands worked for me:

brew reinstall postgresql
brew postgresql-upgrade-database
2

This had helped me: uninstall all instances of readline and PostgreSQL:

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

And install PostgreSQL.

1
  • Is this a bogus answer? How does setting some C++ compiler flags help or answer the question? At the very least, a whole lot of context is missing. Apr 10 at 3:08
1

I found this solution helpful

brew upgrade bash

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

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
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.

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.

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 Homebrew since OS X v10.11 (El Capitan) had some security upgrade that affects my /usr folder. During the Homebrew 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.