Connect to a older Postgresql database - postgresql

I have PostgreSQL installed in my Linux machine, I'm following a old YT tutorial for work and the instructor is using PostgreSQL 11. I have already used the following command:
sudo apt-get -y install postgresql-11
To install PostgreSQL 11 in my machine, how do I connect to this specific version and not version 12?

Postgress configuration files are located at:
/etc/postgresql/##/main/postgresql.conf
Use your favorite code editor and navigate to the CONNECTIONS AND AUTHENTICATION this will reveal at which port the Postgress database is listening.
Connect to you desired version using the -p flag. For me Postgres 11 is using port 5433 and Postgres 12 is using 5432.
psql -h 5433 postgress
Check your version using
SHOW version();
Expected output
version
----------------------------------------------------------------------------------------------------------------------------
PostgreSQL 11.9 (Ubuntu 11.9-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0, 64-bit
(1 row)

The 2nd installed version is probably running on port 5433 rather than 5432.
But rather than guessing, you can run pg_lsclusters and see. Then specify that port number whereever you need to connect.

Related

Should I remove psql after postgres uninstalling from Mac M1?

I am trying to remove postgresql completely from my Mac M1 and then install postgres 13.
There were postgres 14, postgres 11 and postgres 13 installed (simultaneously) on my computer. Due to versions conflict I decided to remove all my postgreses and install version 13 de novo.
To uninstall postgres completely, I followed these recommendations: https://blog.testdouble.com/posts/2021-01-28-how-to-completely-uninstall-homebrew-postgres/
Now I have:
% postgres --version
zsh: command not found: postgres
% which postgresql
postgresql not found
% psql testdb
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory == NO DATABASES
%brew list also shows no postgres installed
but when I do
% psql --version
I see:
psql (PostgreSQL) 14.1
and
% which psql
/opt/homebrew/opt/libpq/bin/psql
So, I have front-end PostgreSQL 14 until now.
Is it normal that psql was not uninstalled with postgres server?
Of course some postgres files also remain in cache, as well as some Docker postgres images.
Should I remove postgresql14.1 client manually? Here I read that in such case after re-installation I can expect Error.
Or, in general: am I missing something that will prevent my computer from correct installing and running Postgres 13 locally and outside of Docker?
Thank you so much in advance for clarification.
Yes, Postgres client (psql) and PostgreSQL server are two separate software. Quite often you need psql in your workstation even if server is running somewhere else.
But if you don't need to connect any PostgreSQL server anymore from your workstation, you can remove psql. No harm done. And it is always easy to re-install if needed.

PostgreSQL: How to unify versions?

On my Kubuntu machine, locally I was running PostgreSQL in a user-defined directory. Actually, an upgrade from Kubuntu 18.04 to 20.04 provided PostgreSQL12. Actually, I have:
$ psql --version
psql (PostgreSQL) 12.6 (Ubuntu 12.6-0ubuntu0.20.04.1)
$sudo -u postgres psql
[sudo] Passwort:
psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1), server 10.16 (Ubuntu 10.16-0ubuntu0.18.04.1))
What do I have to do, to unify this (to version 12) without losing data in the user-defined directory? There are two version 10 packages (PostgreSQL-10 and PostgreSQL-client-10) and the same ones version 12: Is it OK and sufficient to remove the two from 10?
Thank you!
PostgreSQL is free to change how it stores data between versions. So just moving/copying the data directory is highly unlikely to be sufficient.
The simplest solution is to basically dump the data and then load it into the new server. If you start your new server running on a different port and different data directory, you can do this as one-liner.
pg_dumpall -p 5432 | psql -d postgres -p 5433
https://www.postgresql.org/docs/12/upgrading.html#UPGRADING-VIA-PGDUMPALL
If you have a lot of data, and you need to keep your database available during the upgrade then you may wish to look into logical replication.

psql version different depending on how you check it

The system says it has psql version 13 in 3 places, but seems to be version 11:
The three places:
> psql --version
psql (PostgreSQL) 13.1 (Ubuntu 13.1-1.pgdg20.04+1)
> pg_config --version
PostgreSQL 13.1 (Ubuntu 13.1-1.pgdg20.04+1)
> psql -U postgres
psql (13.1 (Ubuntu 13.1-1.pgdg20.04+1), server 11.10 (Ubuntu 11.10-1.pgdg20.04+1))
Type "help" for help.
postgres=#
In postgres if I run SELECT version(); it says 11:
postgres=# SELECT version();
version
------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 11.10 (Ubuntu 11.10-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, 64-bit
(1 row)
I was getting this error, but changes to /etc/postgresql/13/main/pg_hba.conf (13) had no effect on psql command, whereas changes to /etc/postgresql/11/main/pg_hba.conf (11) did.
What is going on? Is there some strange nuance of psql?
Apparently you have two Postgres versions installed (13 and 11).
The PATH variable contains the binaries (psql, pg_config) for version 13, but version 11 is running on the default port 5432. Which also explains why you need to change pg_hba.conf for the version 11 to see a difference in the connection behaviour.
If you want to connect to the 13 instance, you need to provide the port for that. Try e.g. psql -p 5433 -U postgres - typically a second installation defaults to the next higher port number

Parallel versions in PostgreSQL, What do I actually use?

Os - ubuntu 18.04 (bionic)
I have installed postgresql 10 some time ago, and today i install a new version, 12.
I don't mind to throw all the previous data, so i tried to do pg_ctl stop for the old server. I am not able to execute the pg_ctl stop for the old server, because the command not found.
I found where postgres is installed, under /var/lib/postgres and it has two folders, one is 10 and the other is 12. I tried to call pg_ctl from 10/main but it does not appear also there. there is no bin folder there as well.
When I type psql under the postgres user, i get this info:
psql (12.2 (Ubuntu 12.2-2.pgdg18.04+1), server 10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
I have search for hours in the docs and on the web, but i didn't find info for that.
Can you help me understand what is going on in my machine?
Edit: pg_lsclusters output:
Ver Cluster Port Status Owner Data directory Log file
10 main 5432 online postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
12 main 5433 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
also, i can no longer connect to the new port. I have try to connect to port 5433 with postgres user and my password ( I have changed the pg_hba.conf row to authenticate with md5), but it didn't work.

how launch PSQL if many versions of postgresql

I have ubuntu 18.04 x64 on which i installed PostgreSQL10 and PostgreSQL9.6
When type psql, i have psql for PostgreSQL10.
How to launch psql for PostgreSQL9.6 ?
At finish, i would like to set up PGadmin 3 for those two postgresql instances
There is no need to use psql in version 9.6 for a PostgreSQL 9.6 server. The clients are always downwards compatible.
Best practice is, to use always the latest client, no matter what version of server you are running.