psql version different depending on how you check it - postgresql

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

Related

Upgrading postgresql v9.4 to v12 but get pg_upgrade: command not found

I'm on Ubuntu 20.04 and currently running my app on postgresql 9.4.
I installed postgres version 12 and I'm trying to migrate my data over from postgres v9.4 to postgres v12. I also have postgres version 11 installed from an earlier time.
I'm trying to do this from pg_upgrade
So when I run pg_lsclusters I get:
9.4 main 5432 online postgres /var/lib/postgresql/9.4/main /var/log/postgresql/postgresql-9.4-main.log
11 main 5433 down postgres /var/lib/postgresql/11/main /var/log/postgresql/postgresql-11-main.log
12 main 5434 down postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
When change to the postgres user (from the root) and run pg_upgrade --check I get:
pg_upgrade: command not found
Where is it looking for the pg_upgrade file/command?

Why different versions for PostgreSQL displayed?

When I execute in terminal
psql -V
It outputs:
psql (PostgreSQL) 13.2 (Ubuntu 13.2-1.pgdg18.04+1)
When I execute in psql prompt
SELECT version();
It outputs:
PostgreSQL 11.11 (Ubuntu 11.11-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0, 64-bit
What does it mean? Why there is different version? Which is the actual version? How do I make sure both have same version?
psql -v gives you the version of the (client side) command line tool (running on your computer).
select version(); gives your the version of the server you are connected to.
Your output means you are using psql 13 to connect to a database server running PostgreSQL 11.
If you want both to be the same version you either need to downgrade your local installation or upgrade the server to 13 (which is what I would do)

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.

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.

Getting pg_restore error 1 when running pg:pull on Heroku

I am trying to copy the database from our staging server onto my local machine and I'm having some issues. This was working 2 months ago flawlessly and when I tried to use it again today I'm getting an issue.
I am using this command:
heroku pg:pull DATABASE_URL my_app_db -a myApp
And at the end of the logs I get this:
pg_dump: reading subscriptions
pg_dump: reading large objects
pg_dump: reading dependency data
pg_dump: saving encoding = UTF8
pg_dump: saving standard_conforming_strings = on
pg_dump: saving search_path =
pg_dump: saving database definition
pg_restore: [archiver] unsupported version (1.14) in file header
pg_dump: dumping contents of table "public.addresses"
▸ pg_restore errored with 1
I've tried updating my postgresql package but they're the same on Heroku and my local machine:
$ psql --version
psql (PostgreSQL) 12.3 (Ubuntu 12.3-1.pgdg18.04+1)
$ heroku run psql --version -a myApp
Running psql --version on ⬢ myApp... up, run.5796 (Hobby)
psql (PostgreSQL) 12.3 (Ubuntu 12.3-1.pgdg18.04+1)
I've always dropped the database locally before doing the pull:
$ psql postgres
postgres=# DROP DATABASE my_app_db;
postgres=# \q
Update 1:
Looks like I have a pg_dump and pg_pull version mismatch:
$ pg_dump --version
pg_dump (PostgreSQL) 10.13 (Ubuntu 10.13-1.pgdg18.04+1)
$ heroku run pg_dump --version -a myApp
pg_dump (PostgreSQL) 12.3 (Ubuntu 12.3-1.pgdg18.04+1)
$ pg_restore --version
pg_restore (PostgreSQL) 10.13 (Ubuntu 10.13-1.pgdg18.04+1)
$ heroku run pg_restore --version -a myApp
pg_restore (PostgreSQL) 12.3 (Ubuntu 12.3-1.pgdg18.04+1)
What I saw on the internet is that it should be updated by installing the latest postgresql. For some reason two of them aren't updated even with the latest PostgreSQL version.
Solution to resolve the issue:
As clemoun pointed out, I must've had multiple psql versions running. I saw what I have with:
$ pg_lsclusters
Then uninstalled version 10 and 11 with:
$ sudo apt-get remove postgresql-10 postgresql-11
After that dropped the clusters:
$ sudo pg_dropcluster 10 main
$ sudo pg_dropcluster 11 main
And logged out and in.
The fact that you have at the same time :
$ psql --version
psql (PostgreSQL) 12.3 (Ubuntu 12.3-1.pgdg18.04+1)
and
$ pg_restore --version
pg_restore (PostgreSQL) 10.13 (Ubuntu 10.13-1.pgdg18.04+1)
makes me think you have several versions of PostgreSQL installed on your computer.
Try typing :
sudo find /usr -wholename '*/bin/postgres'
and you might find out you have both PostgreSQL versions 10.13 and 12.3 installed.
For some reason it's the 10.13 version of pg_restore that runs and Heroku requires that you use a version above 10.3
=> Your 12.3 version should do the trick
Try uninstalling version 10.13 of Postgresql, leaving you computer with version 12.3 only. Thus pg_restore will run on version 12.3