46

I am trying to execute pg_dump on PostgreSQL 9.0.4 server running on Debian and I am getting the error below:

./pg_dump: error while loading shared libraries: libpq.so.5: cannot open shared object file: No such file or directory 

libpq.so.5 is a link to libpq.so.5.3 as shown below

lrwxrwxrwx 1 root root     12 Jun 27 16:24 libpq.so.5 -> libpq.so.5.3
-rwxr-xr-x 1 root root 180749 Jun 21 02:43 libpq.so.5.3

What is it that I am doing wrong?

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
McKibet
  • 633
  • 1
  • 5
  • 12

9 Answers9

42

Try this:

1: Know the path of libpq.so.5

find / -name libpq.so.5

Output example: /usr/pgsql-9.4/lib/libpq.so.5 If found nothing, check if you have already installed the suitable postgresql-libs for your postgresql version and your OS platform

2: Symbolic link that library in a "well known" library path like /usr/lib:

ln -s /usr/pgsql-9.4/lib/libpq.so.5 /usr/lib/libpq.so.5

Attention: If your platform is 64 bit, you MUST also symbolic link to 64 bit libraries path:

ln -s /usr/pgsql-9.4/lib/libpq.so.5 /usr/lib64/libpq.so.5

3: Be happy !

Jan Nielsen
  • 10,892
  • 14
  • 65
  • 119
Yahya Yahyaoui
  • 2,833
  • 23
  • 30
  • 1
    All postgress utilities were failing with missing libp until I declared LD_LIBRARY_PATH. But then pg_dump failed during import when some other library was loaded. Your hint with ln to well known location solved this issue. – Leos Literak Jul 16 '20 at 13:46
  • 1
    Still getting the same error after created sym links – Chaminda Bandara Dec 31 '21 at 02:11
13

Debian-based Linux distros

Since this is the top search result for the error. I'll add an updated answer. I received the error when trying to start a django server.

I hadn't installed the postgres stuff.

Try:

sudo apt install libpq-dev

# or
sudo apt install libpq5

See:

Explanation

Missing the libpq dependency is most likely due to installing psycopg2 via pip instead of apt. Thus, to avoid any problems with Debian-based distros like Ubuntu and Raspberry Pi OS, always do as follows:

sudo apt install python3-psycopg2

Which will nicely pull all the needed dependencies, among which there is libpq

alfx
  • 174
  • 2
  • 14
jmunsch
  • 22,771
  • 11
  • 93
  • 114
12

In which directory are these libpq files? You can try setting environment variable LD_LIBRARY_PATH to point to this directory or make sure it's in standard place.

Also, why isn't the libpq.so.5 link shown in the "as shown below" section? Maybe you should just run ldconfig?

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
9

I was getting the same error message on Postgres 9.5 on RHEL 6.5 which lead me to this post. But a find for the file libpq.so.5 returned nothing, which made things more confusing.

In the end the following symbolic links made it run

ln -s /opt/rh/rh-postgresql95/root/usr/lib64/libpq.so.rh-postgresql95-5  /usr/lib64/libpq.so.rh-postgresql95-5 
ln -s /opt/rh/rh-postgresql95/root/usr/lib64/libpq.so.rh-postgresql95-5  /usr/lib/libpq.so.rh-postgresql95-5

These paths are for RHEL, use find / -name libpq.so to location your installation and add it to the same destination folders /usr/lib/ and /usr/lib64/ using the orginal file name.

The root cause appears that the installation did not place this file into a shared location.

oden
  • 3,461
  • 1
  • 31
  • 33
2

This error probably occurs because of $LD_LIBRARY_PATH environment variable is not set.

When you install your application from source code using prefix (./configure --prefix=/some/path), you have to inform where your lib/ path is. I just found a solution for this, and I added this variable to postgres user init bash script:

printf 'export PATH=$PATH:/opt/apl/pgsql/bin\nexport LD_LIBRARY_PATH=/opt/apl/pgsql/lib:$LD_LIBRARY_PATH\n' > /etc/profile.d/postgres.sh
Joabe Lucena
  • 792
  • 8
  • 21
2

redhat 7 is missing few steps after installing yum install pgadmin4:

sudo ln -s /usr/lib64/pgdg-libpq5/lib/libpq.so /usr/lib64/libpq.so.5
sudo ln -s /usr/lib64/pgdg-libpq5/lib/libpq.so /usr/lib/libpq.so.5
sudo chown -R apache:apache  /var/lib/pgadmin4/

then you can run

sudo python3 /usr/lib/python3.6/site-packages/pgadmin4-web/setup.py

and if all successful:

systemctl start httpd
systemctl status httpd
apachectl configtest

and make sure the httpd starts ok

taitelman
  • 612
  • 5
  • 9
0

I had exactly the same problem with the pg 12 reinstall when recovering from a storage failure. I fixed it by installing the postgressql shared libraries

 yum  install postgresql12-libs
0

This must have happened due to mismatched dependencies

I have used

sudo apt --fix-broken install

And after

sudo apt install libpq-dev

This worked to solve my problem.

Marco_Nunes
  • 1
  • 1
  • 2
-1

I had exactly the same problem with the pg 9.6 install. I fixed it like this. Rather irritating that the installer doesn't factor this in.

***********post yum install & running initdb *********
Success. You can now start the database server using:

    /opt/rh/rh-postgresql96/root/usr/bin/pg_ctl -D /var/opt/rh/rh-postgresql96/lib/pgsql/data -l logfile start

-bash-4.2$ /opt/rh/rh-postgresql96/root/usr/bin/pg_ctl -D /var/opt/rh/rh-postgresql96/lib/pgsql/data -l logfile start
/opt/rh/rh-postgresql96/root/usr/bin/pg_ctl: **error while loading shared libraries: libpq.so.rh-postgresql96-5: cannot open shared object file: No such file or directory**

-bash-4.2$ id
uid=26(postgres) gid=26(postgres) groups=26(postgres) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023

**************
-bash-4.2$ cat LibFix
ln -s /opt/rh/rh-postgresql96/root/usr/lib64/libpq.so.rh-postgresql96-5  /usr/lib64/libpq.so.rh-postgresql96-5
ln -s /opt/rh/rh-postgresql96/root/usr/lib64/libpq.so.rh-postgresql96-5  /usr/lib/libpq.so.rh-postgresql96-5
**************

[root@****lab ~]# ln -s /opt/rh/rh-postgresql96/root/usr/lib64/libpq.so.rh-postgresql96-5  /usr/lib64/libpq.so.rh-postgresql96-5
[root@****lab ~]# ln -s /opt/rh/rh-postgresql96/root/usr/lib64/libpq.so.rh-postgresql96-5  /usr/lib/libpq.so.rh-postgresql96-5

[root@****lab ~]# su - postgres
Last login: Thu Apr  5 08:57:21 CEST 2018 on pts/0

-bash-4.2$ /opt/rh/rh-postgresql96/root/usr/bin/pg_ctl -D /var/opt/rh/rh-postgresql96/lib/pgsql/data -l logfile start
server starting

-bash-4.2$ ps -ef | grep postgres
root     12778  7883  0 09:07 pts/0    00:00:00 su - postgres
postgres 12779 12778  0 09:07 pts/0    00:00:00 -bash
postgres 12802     1  0 09:08 pts/0    00:00:00 /opt/rh/rh-postgresql96/root/usr/bin/postgres -D /var/opt/rh/rh-postgresql96/lib/pgsql/data
postgres 12803 12802  0 09:08 ?        00:00:00 postgres: logger process
postgres 12805 12802  0 09:08 ?        00:00:00 postgres: checkpointer process
postgres 12806 12802  0 09:08 ?        00:00:00 postgres: writer process
postgres 12807 12802  0 09:08 ?        00:00:00 postgres: wal writer process
postgres 12808 12802  0 09:08 ?        00:00:00 postgres: autovacuum launcher process
postgres 12809 12802  0 09:08 ?        00:00:00 postgres: stats collector process
postgres 12810 12779  0 09:08 pts/0    00:00:00 ps -ef

-bash-4.2$ id
uid=26(postgres) gid=26(postgres) groups=26(postgres) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
-bash-4.2$ psql
psql (9.6.5)

postgres=# \conninfo

You are connected to database "postgres" as user "postgres" via socket in "/var/run/postgresql" at port "5432".
Fovas Denis
  • 23
  • 1
  • 6
Ade
  • 1
  • 1