PostgreSQL: How to unify versions? - postgresql

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.

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.

Connect to a older Postgresql database

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.

pg_dump version mismatch

I have a remote postgres DB version 10.8 setup on a linux. I am trying to back it up on windows machine using postgres 9.6.12
pg_dump --host dahost --port 5432 --username "postgres" --no-password --format tar --blobs --verbose --file "path\noury.backup" "highlands_wastewater"
I get these errors
pg_dump: server version: 10.8 (Ubuntu 10.8-0ubuntu0.18.04.1); pg_dump version: 9.6.12
pg_dump: aborting because of server version mismatch
How can I backup this database remotely?
As the error message says, you cannot use an older version of pg_dump with a newer PostgreSQL Version.
The reason behind that is that PostgreSQL 9.6 cannot know how to dump a v10 database properly — there may be new features in the newer version (for example, partitioned tables).
You'll have to install PostgreSQL v10 or later on your Windows machine (exactly v10 if you want to be able to restore the dump to the original database).
Don't use the tar format. The custom format offers more advantages.

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.

postgresql upgrade backup and restore same port

Here, I want to upgrade postgresql from 8.3(port 5432) to 9.0(port 5433)
Whats happen if,
-First I instal the postgres new version 9.0 on port 5433
-Than I backup and restore database by using comman,
/usr/local/pgsql/bin/ pg_dumpall -U -h -p 5432|psql -U -p 5433 -h
-After that I stop both server 8.3 and 9.0
-By using pgAdmin III, I change port the server 9.0 from 5433 to 5432.
Whats happen if I try to upgrade with that way?
thanks :)
This would work. This should also be faster than writing to file, as less writes will be needed. But remember to put old database in single user mode (postgres --single ...) so nobody would write to the database during migration.
You can also use pg_upgrade to upgrade database in place. But check for limitations while upgrading from 8.3.
As Tometzky has already said, this should work.
Just to make the picture complete:
With 9.0 you can also use pg_upgrade for the migration which should be even faster than using pg_dump with a pipe.