How do I avoid starting multiple postgres instances on my machine - postgresql

Why do I see multiple postgres instances in my machine. I would like to stop + remove the older version of postgres permanently, how should I go about doing this.
ps -ef | grep postgresql
postgres 1766 1 0 06:08 ? 00:00:00 /usr/lib/postgresql/9.1/bin/postgres -D /var/lib/postgresql/9.1/main -c config_file=/etc/postgresql/9.1/main/postgresql.conf -c external_pid_file=/var/run/postgresql/9.1-main.pid
postgres 3398 1 0 06:09 ? 00:00:00 /usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/9.4/main -c config_file=/etc/postgresql/9.4/main/postgresql.conf

To remove, uninstall the postgres version via your package manager.
Ex: sudo apt-get remove postgresql-9.1
To temporarily disable, follow this:
https://serverfault.com/questions/582499/i-have-multiple-postgresql-servers-installed-how-do-i-disable-auto-boot-for-old

Related

PostgreSQL - WSL : won't init

i'm currently developping a Django server where i need a database, Sqlite3 don't seem to handle well my need so i'm trying to use PostgreSQL instead. I'm devellopping on Windows but the server will run on a Linux system once development is done, so i'm trying to use WSL to fit the expected result.
wsl -l -v
NAME STATE VERSION
Ubuntu-22.04 Running 1
I also updated to WSL2, same issue.
My issue is that i can't start the database :
# sudo service postgresql start
No PostgreSQL clusters exist; see "man pg_createcluster"
What i've done so far :
sudo apt update
sudo apt upgrade
sudo apt install postgresql postgresql-contrib
// it s installed
> psql --version
psql (PostgreSQL) 14.2 (Ubuntu 14.2-1ubuntu1)
// no service currently running
> ps aux| grep postgres
root 583 0.0 0.0 15044 1280 tty3 S 08:42 0:00 grep --color=auto postgres
// no cluster ?!?
> sudo service postgresql start
No PostgreSQL clusters exist; see "man pg_createcluster"
// indeed no cluster are created by default
> pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
// but i can t create one
> pg_createcluster 14.2 main
Error: no initdb program for version 14.2 found
> sudo pg_createcluster 14 main
Creating new PostgreSQL cluster 14/main ...
/usr/lib/postgresql/14/bin/initdb -D /var/lib/postgresql/14/main --auth-local peer --auth-host scram-sha-256 --no-instructions
Can't exec "/usr/lib/postgresql/14/bin/initdb": Permission denied at /usr/bin/pg_createcluster line 86.
Error: Could not open /etc/postgresql/14/main/start.conf for writing: Permission denied
Can't exec "/bin/sh": Permission denied at /usr/bin/pg_createcluster line 617.
Error: initdb failed
I also tried : this stackoverflow link
> sudo service --status-all
// ...
[ - ] postgresql
// ...
> sudo ps aux | grep postgres
root 769 0.0 0.0 15044 1284 tty3 S 08:48 0:00 grep --color=auto postgres
I didn't really understand this link : Change some config file that i can't find
And this one that only proposed to reformat my disk.. Link
Following Mark's Link i was able to find a solution.
It seems that WSL2 is required
I tried 2 wsl distro :
Ubuntu-22.04 -> Didn't work even with the guide, maybe it would work on a fresh install.
Ubuntu 20.04.4 LTS -> fresh install worked
# Create the file repository configuration:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Import the repository signing key:
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
# Update the package lists:
sudo apt-get update
# Install the latest version of PostgreSQL.
# If you want a specific version, use 'postgresql-12' or similar instead of 'postgresql':
sudo apt-get -y install postgresql postgresql-contrib

Is there any alternative to the ```\d+``` to describe database schema in PGSQL 14?

I want to describe a table schema in postgresql 14.
However when I use the older \d+, the error is thrown:
pgsql=# \d+ <table_name>
ERROR: column c.relhasoids does not exist
LINE 1: ...riggers, c.relrowsecurity, c.relforcerowsecurity, c.relhasoi...
Thanks in advance!
If you use an older psql version to connect to a newer server version, some commands may fail, as psql informs you when the connection is established. This is exactly what you see at work there.
Call the psql from your v14 installation, and things will work fine. If you don't know how to do that, uninstall all older versions of PostgreSQL.
\d+ is working on Postgres14 well. Maybe you are use older version of psql. There is not forward compatibility. If you want to use PostgreSQL14, you should to use psql for Postgres14 and higher.
You can check it by --option:
[pavel.stehule#pgsql ~]$ psql --version
psql (PostgreSQL) 9.6.23
Like both the earlier answers, you have two versions of psql in your host and the default is the older one which is not compatible with the current database version which is 14.
Temporary fix:
verify the current version & location
bash-4.2$ psql --version
psql (PostgreSQL) 9.2.24
bash-4.2$ which psql
/usr/bin/psql
Find the location of the version of psql for the current DB.
Usually, its under /usr/psql??/bin , like:
bash-4.2$ cd /usr/pgsql-13/bin
bash-4.2$ ls -ltr|grep psql
-rwxr-xr-x 1 root root 644264 May 13 2021 psql
Navigate to that location or invoke that psql binary by providing the path, like
bash-4.2$ /usr/pgsql-13/bin/psql --version
psql (PostgreSQL) 13.3
bash-4.2$ /usr/pgsql-13/bin/psql
Pager usage is off.
psql (13.3)
Type "help" for help.
postgres=#
Permanent Fix
If you want the psql of the existing database which is the new version to be default , you can use 'update-alternatives' utility. Like mentioned here:
How do I correct the mismatch of psql version and postgresql version?
This is how I did.
[root#ip-10-211-209-252 bin]# pwd
/usr/bin
[root#ip-xxxx bin]# mv psql psql9
[root#ip-xxxx bin]# update-alternatives --install /usr/bin/psql psql /usr/bin/psql9 1
[root#ip-xxxx bin]# cd /usr/pgsql-13/bin
[root#ip-xxxx bin]# ls -ltr|grep psql
-rwxr-xr-x 1 root root 644264 May 13 2021 psql
[root#ip-xxxx bin]# update-alternatives --install /usr/bin/psql psql /usr/pgsql-13/bin/psql 2
[root#ip-xxxx bin]# update-alternatives --config psql
There are 3 programs which provide 'psql'.
Selection Command
-----------------------------------------------
*+ 1 /usr/pgsql-13/bin/psql
2 /usr/bin/psql
3 /usr/bin/psql9
Enter to keep the current selection[+], or type selection number: 1
[root#ip-xxxx bin]#

Difference between /usr/lib/ and /usr/local/ in installation of PostgreSQL?

I am reading this thread and the manual of PostgreSQL 9.4.
I run
ps auxw | grep postgres | grep -- -D
but get
postgres 17340 0.0 0.5 226700 21756 ? S 09:50 0:00 /usr/lib/postgresql/9.4/bin/postgres -D /var/lib/postgresql/9.4/main -c config_file=/etc/postgresql/9.4/main/postgresql.conf
but the default installation location should be /usr/local/pgsql/ according to the manual at file:///usr/share/doc/postgresql-doc-9.4/html/install-short.html.
It may be possible that the database was initdb in the /usr/lib/ directory.
How can you change the installation location of PostgreSQL?
The thread you're reading says that the installation directory is /usr/local/pgsql when you install from source, so I suppose this is what you do as well. To change the directory that make install uses for installation, you need to specify the --prefix argument to configure:
./configure --prefix=/path/to/install
make
sudo make install
PS: in case you don't install from source, refer to the documentation which comes with your package manager.

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

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?
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 !
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?
Ubuntu 21.10+
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
See:
https://packages.ubuntu.com/impish/libpq-dev
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.
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
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
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".

Can't connect the postgreSQL with psycopg2

It's the first time that I can't find the answer about some tech problems
Here's my problems:
>> conn=psycopg2.connect(database="mydb", user="postgres", password="123",port=5432)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
psycopg2.OperationalError: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
My postgreSQL is running
My listeningport is 5432 for sure
root#lanston-laptop:~# psql -l
Password:
List of databases
Name | Owner | Encoding | Collation | Ctype | Access privileges
---------------+----------+----------+------------+------------+-----------------------
checkdatabase | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
mydb | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
Thanks a lot!
Your libpq, which is used by psycopg2 expects Postgres socket to be in /var/run/postgresql/ but when you install Postgres from source it is by default it in /tmp/.
Check if there is a file /tmp/.s.PGSQL.5432 instead of /var/run/postgresql/.s.PGSQL.5432. Try:
conn=psycopg2.connect(
database="mydb",
user="postgres",
host="/tmp/",
password="123"
)
Only this solved my problem,
make a symbolic link to the /tmp/.s.PGSQL.5432:
sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
Thanks to, Sukhjit Singh Sehra - s-postgresql-server-is-running
I originally intended to make it a comment to Tometzky's answer, but well, I have a lot to say here... Regarding the case where you don't call psycopg2.connect directly, but use third-party software.
tl;dr
Set unix_socket_directories in postgresql.conf to /var/run/postgresql, /tmp, and restart PostgreSQL.
intro
I tried PostgreSQL 9.2 (CentOS 7) and 9.5 (Ubuntu Xenial) from distro repos, PostgreSQL 9.3, 9.4, 9.5, 9.6, 10 on CentOS 7 from PostgreSQL repo, PostgreSQL 9.6, 10 on Ubuntu Xenial from PostgreSQL repo. Among them only 9.3 listens to only /tmp:
$ systemctl stop postgresql-9.4 && systemctl start postgresql-9.3
$ lsof -aUp $(ps --ppid 1 -o pid= -o comm= | awk '$2 == "postgres" || $2 == "postmaster" {print $1}')
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 25455 postgres 4u unix 0xffff9acb23bc5000 0t0 6813995 /tmp/.s.PGSQL.5432
$ systemctl stop postgresql-9.3 && systemctl start postgresql-9.4
$ lsof -aUp $(ps --ppid 1 -o pid= -o comm= | awk '$2 == "postgres" || $2 == "postmaster" {print $1}')
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
postgres 26663 postgres 4u unix 0xffff9ac8c5474c00 0t0 7086508 /var/run/postgresql/.s.PGSQL.5432
postgres 26663 postgres 5u unix 0xffff9ac8c5477c00 0t0 7086510 /tmp/.s.PGSQL.5432
python-psycopg2
That's not a big deal with psql, just a matter of running the matching binary. But if you, for instance, have python-psycopg2 installed from CentOS's base or update repo. It links dynamically to libpq that OS provides. With 9.3 and 9.4 installed OS provides 9.4's version:
$ alternatives --display pgsql-ld-conf
pgsql-ld-conf - status is auto.
link currently points to /usr/pgsql-10/share/postgresql-9.4-libs.conf
/usr/pgsql-9.3/share/postgresql-9.3-libs.conf - priority 930
/usr/pgsql-9.4/share/postgresql-9.4-libs.conf - priority 940
Current `best' version is /usr/pgsql-9.4/share/postgresql-9.4-libs.conf.
$ ls -l /etc/ld.so.conf.d
lrwxrwxrwx 1 root root 31 Feb 7 02:25 postgresql-pgdg-libs.conf -> /etc/alternatives/pgsql-ld-conf
$ ls -l /etc/alternatives/pgsql-ld-conf
lrwxrwxrwx 1 root root 43 Feb 7 02:25 /etc/alternatives/pgsql-ld-conf -> /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
$ cat /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
/usr/pgsql-9.4/lib/
But libpq that comes with PostgreSQL 9.4 looks for socket in /var/run/postgresql as opposed to 9.3:
$ strings /usr/pgsql-9.3/lib/libpq.so.5 | egrep '/(tmp|var)'
/tmp
$ strings /usr/pgsql-9.4/lib/libpq.so.5 | egrep '/(tmp|var)'
/var/run/postgresql
The solution comes from postinstall scripts of corresponding packages:
$ yum reinstall --downloadonly postgresql94-libs
$ rpm -qp /var/cache/yum/x86_64/7/pgdg94/packages/postgresql94-libs-9.4.15-1PGDG.rhel7.x86_64.rpm --scripts
postinstall scriptlet (using /bin/sh):
/usr/sbin/update-alternatives --install /etc/ld.so.conf.d/postgresql-pgdg-libs.conf pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf 940
/sbin/ldconfig
# Drop alternatives entries for common binaries and man files
postuninstall scriptlet (using /bin/sh):
if [ "$1" -eq 0 ]
then
/usr/sbin/update-alternatives --remove pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
/sbin/ldconfig
fi
Temporarily remove 9.4's alternative:
$ alternatives --remove pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
$ ldconfig
When finished either reinstall postgresql94-libs, or add the alternative back:
$ alternatives --install /etc/ld.so.conf.d/postgresql-pgdg-libs.conf pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf 940
$ ldconfig
pip
If you install psycopg2 with pip on the other hand, it by default installs precompiled package which comes with its own libpq, which looks for socket in /var/run/postgresql:
$ python3.5 -m venv 1
$ . ./1/bin/activate
(1) $ pip install psycopg2
(1) $ python
>>> import psycopg2
>>>Ctrl-Z
[1]+ Stopped python
(1) $ pgrep python
26311
(1) $ grep libpq /proc/26311/maps | head -n 1
7f100b8cb000-7f100b90e000 r-xp 00000000 08:04 112980 /home/yuri/1/lib/python3.5/site-packages/psycopg2/.libs/libpq-909a53d8.so.5.10
(1) $ strings /home/yuri/1/lib/python3.5/site-packages/psycopg2/.libs/libpq-909a53d8.so.5.10 | egrep '/(tmp|var)'
/var/run
/var/run/postgresql
The solution is to ask pip to not install precompiled package, and make pg_config of the proper version of PostgreSQL available:
$ PATH=/usr/pgsql-9.3/lib:$PATH pip install --no-binary psycopg2 psycopg2
You can even add --no-binary switch to requirements.txt:
psycopg2==2.7.3.2 --no-binary psycopg2
unix_socket_directories
The easier option though is to make use of unix_socket_directories option:
Try change port to 5433 instead of 5432
a few years later, using the EnterpriseDB 'graphical' install on OSX 10.8, and pip install of psycopg2 (after linking the /Library/...dylib's as described here) i had this same issue.
for me the correct connect command was conn = psycopg2.connect('dbname=DBNAME user=postgres password=PWHERE host=/tmp/')
In my case with a conda installation had to: sudo ln -s /var/run/postgresql/.s.PGSQL.5432 /tmp/.s.PGSQL.5432
Having this happen to me after a brew upgrade, I googled for brew .s.PGSQL.5432.
Per the suggestion in this answer I ran the following:
postgres -D /usr/local/var/postgres
And got:
2019-10-29 17:43:30.860 IST [78091] FATAL: database files are incompatible with server
2019-10-29 17:43:30.860 IST [78091] DETAIL: The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.5.
I googled that FATAL error and per the suggestion in this answer I ran:
brew postgresql-upgrade-database
That solved it for me.
put vpc_access_connector:
name: project//locations/us-central1/connectors/
and host :'/cloudsql/::
It should work for private Ip postgresql on gcp
Try this once
cd /etc/postgresql/13/main
vi pg_hba.conf
Change the line after this
Database administrative login by Unix domain socket
local all postgres peer
To
local all postgres md5
Then execute following commands
sudo systemctl stop postgresql
sudo systemctl start postgresql
Then run the python program then it will work properly