Which version of PostgreSQL am I running? - linux

I'm in a corporate environment (running Debian Linux) and didn't install it myself. I access the databases using Navicat or phpPgAdmin (if that helps). I also don't have shell access to the server running the database.

Run this query from PostgreSQL:
SELECT version();

I believe this is what you are looking for,
Server version:
pg_config --version
Client version:
psql --version

Using CLI:
Server version:
$ postgres -V # Or --version. Use "locate bin/postgres" if not found.
postgres (PostgreSQL) 9.6.1
$ postgres -V | awk '{print $NF}' # Last column is version.
9.6.1
$ postgres -V | egrep -o '[0-9]{1,}\.[0-9]{1,}' # Major.Minor version
9.6
If having more than one installation of PostgreSQL, or if getting the "postgres: command not found" error:
$ locate bin/postgres | xargs -i xargs -t '{}' -V # xargs is intentionally twice.
/usr/pgsql-9.3/bin/postgres -V
postgres (PostgreSQL) 9.3.5
/usr/pgsql-9.6/bin/postgres -V
postgres (PostgreSQL) 9.6.1
If locate doesn't help, try find:
$ sudo find / -wholename '*/bin/postgres' 2>&- | xargs -i xargs -t '{}' -V # xargs is intentionally twice.
/usr/pgsql-9.6/bin/postgres -V
postgres (PostgreSQL) 9.6.1
Although postmaster can also be used instead of postgres, using postgres is preferable because postmaster is a deprecated alias of postgres.
Client version:
As relevant, login as postgres.
$ psql -V # Or --version
psql (PostgreSQL) 9.6.1
If having more than one installation of PostgreSQL:
$ locate bin/psql | xargs -i xargs -t '{}' -V # xargs is intentionally twice.
/usr/bin/psql -V
psql (PostgreSQL) 9.3.5
/usr/pgsql-9.2/bin/psql -V
psql (PostgreSQL) 9.2.9
/usr/pgsql-9.3/bin/psql -V
psql (PostgreSQL) 9.3.5
Using SQL:
Server version:
=> SELECT version();
version
--------------------------------------------------------------------------------------------------------------
PostgreSQL 9.2.9 on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-4), 64-bit
=> SHOW server_version;
server_version
----------------
9.2.9
=> SHOW server_version_num;
server_version_num
--------------------
90209
If more curious, try => SHOW all;.
Client version:
For what it's worth, a shell command can be executed within psql to show the client version of the psql executable in the path. Note that the running psql can potentially be different from the one in the path.
=> \! psql -V
psql (PostgreSQL) 9.2.9

If you're using CLI and you're a postgres user, then you can do this:
psql -c "SELECT version();"
Possible output:
version
-------------------------------------------------------------------------------------------------------------------------
PostgreSQL 11.1 (Debian 11.1-3.pgdg80+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 4.9.2-10+deb8u2) 4.9.2, 64-bit
(1 row)

The accepted answer is great, but if you need to interact programmatically with PostgreSQL version maybe it's better to do:
SELECT current_setting('server_version_num'); -- Returns 90603 (9.6.3)
-- Or using SHOW command:
SHOW server_version_num; -- Returns 90603 too
It will return server version as an integer. This is how server version is tested in PostgreSQL source, e.g.:
/*
* This is a C code from pg_dump source.
* It will do something if PostgreSQL remote version (server) is lower than 9.1.0
*/
if (fout->remoteVersion < 90100)
/*
* Do something...
*/
More info here and here.

Execute command
psql -V
Where
V must be in capital.

in shell psql.exe , execute
\! psql -V

Using pgadmin4 it can be seen by double clicking Servers > server_name_here > Properties tab > Version:
Version 3.5:
Version 4.1, 4.5:

A simple way is to check the version by typing psql --version in terminal

In my case
$psql
postgres=# \g
postgres=# SELECT version();
version
---------------------------------------------------------------------------------------------------------------------
PostgreSQL 8.4.21 on x86_64-pc-linux-gnu, compiled by GCC gcc-4.6.real (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3, 64-bit
(1 row)
Hope it will help someone

The pg_config command will report the directory where the PostgreSQL programs are installed (--bindir), the location of C include files (--includedir) and object code libraries (--libdir), and the version of PostgreSQL (--version):
$ pg_config --version
PostgreSQL 9.3.6

use VERSION special variable
$psql -c "\echo :VERSION"

Using command line
Server:
postgres -V
Client:
psql -V
Login to postgres then:
postgres=# select version();
Or from cli:
psql -c "SELECT version();"
Use VERSION special variable
Login as postgres user:
sudo su - postgres
Then:
psql -c "\echo :VERSION"
Check out this guide here for full explaination

If you are already using for tool(used DBeaver) to connect PostgreSQL, it will look like this :

Useful Queries to Chck PostgreSQL Database Version
bash-4.1$ psql
postgres=# SELECT version();
postgres=# SHOW server_version;
To Check PostgreSQL Client Version.
bash-4.1$ psql --version
psql (PostgreSQL) 12.1

Run this query from PostgreSQL:
SELECT version();

If you have shell access to the server (the question mentions op does not have, but in case you have,) on a debian/ubuntu system
sudo apt-cache policy postgresql
which will output the installed version,
postgresql:
Installed: 9.6+184ubuntu1.1
Candidate: 9.6+184ubuntu1.1
Version table:
*** 9.6+184ubuntu1.1 500
500 http://in.archive.ubuntu.com/ubuntu artful-updates/main amd64 Packages
500 http://in.archive.ubuntu.com/ubuntu artful-updates/main i386 Packages
500 http://security.ubuntu.com/ubuntu artful-security/main amd64 Packages
500 http://security.ubuntu.com/ubuntu artful-security/main i386 Packages
100 /var/lib/dpkg/status
9.6+184ubuntu1 500
500 http://in.archive.ubuntu.com/ubuntu artful/main amd64 Packages
500 http://in.archive.ubuntu.com/ubuntu artful/main i386 Packages
where the Installed: <version> is the installed postgres package version.

Don’t know how reliable this is, but you can get two tokens of version fully automatically:
psql --version 2>&1 | tail -1 | awk '{print $3}' | sed 's/\./ /g' | awk '{print $1 "." $2}'
So you can build paths to binaries:
/usr/lib/postgresql/9.2/bin/postgres
Just replace 9.2 with this command.

For the current version of PgAdmin: 4.16 at the time of writing.
Select the DB server whose version you need.
Click on the properties tab in
the right pane.
See screenshot below:

This is quite an old question with many good answers. I found that from version 12 onwards, simply invoking the client tells me what I need to know, but I ran them on the server's shell. Examples below with output.
When I was on version 12:
$ sudo su postgres -c "psql"
psql (12.8 (Ubuntu 12.8-0ubuntu0.20.04.1))
I read this as both the client and the server are at version 12.
After I upgraded Ubuntu from 20.04 to 21.04:
$ sudo su postgres -c "psql"
psql (13.4 (Ubuntu 13.4-0ubuntu0.21.04.1), server 12.8 (Ubuntu 12.8-0ubuntu0.20.04.1))
It's telling me clearly that the client is on version 13 but the server is still on 12, as I confirmed:
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
12 main 5432 online postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
Notice, by the way, this misleading result, at this stage:
$ pg_config --version
PostgreSQL 13.4 (Ubuntu 13.4-0ubuntu0.21.04.1)
After I upgraded to version 14:
$ sudo su postgres -c "psql"
psql (14.0 (Ubuntu 14.0-1.pgdg21.04+1))
Type "help" for help.
postgres=#
Again, I interpret it as both the client and the server being on version 14, confirmed once more:
$ pg_lsclusters
Ver Cluster Port Status Owner Data directory Log file
12 main 5432 down,binaries_missing postgres /var/lib/postgresql/12/main /var/log/postgresql/postgresql-12-main.log
14 main 5433 online postgres /var/lib/postgresql/14/main /var/log/postgresql/postgresql-14-main.log
This version is, by the way, the same obtained by running the SELECT version(); query.

If Select version() returns with Memo try using the command this way:
Select version::char(100)
or
Select version::varchar(100)

Related

How to configure postgres to run only one version

I'm pretty new to using postgresql and seemed to have installed two versions that are running at the same time. Both 12 and 14.
Below is the list of commands that I've run, and the results they've yielded.
From WSL-2 Ubuntu 20.04 CLI
➜ which psql
/usr/bin/psql
➜ pg_config --version
PostgreSQL 14.4 (Ubuntu 14.4-1.pgdg20.04+1)
➜ psql --version
psql (PostgreSQL) 14.4 (Ubuntu 14.4-1.pgdg20.04+1)
From psql
=# SHOW server_version;
server_version
------------------------------------
12.11 (Ubuntu 12.11-1.pgdg20.04+1)
(1 row)
=# SELECT version();
version
-------------------------------------------------------------------------------------------------------------------------------------
PostgreSQL 12.11 (Ubuntu 12.11-1.pgdg20.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0, 64-bit
(1 row)
I've have some persistent issues with my odoo 15 db and think this may be the cause but am unsure.
Any recommendations on how to transition from 12.11 server to 14.4 server and remove 12.11 server would be greatly appreciated.
Thanks to all the above for the help!
Commands I ran to fix on Ubuntu 20.04 / Windows-11 WSL-2:
$ ~ pg_dumpall > export.sql
$ ~ sudo apt-get --purge remove postgresql\*
$ ~ sudo apt-get install postgresql-14 (or -version preferred)
$ ~ sudo -u postgres -i
postgres ~ $ psql
#= CREATE ROLE username superuser
#= ALTER ROLE username WITH LOGIN;
#= \password username;
#= \q
postgres ~ $ exit

how to connect redshift database to a bash script running on ec2 instance

I have couple of sql queries and I want to run them against amazon redhshift , The base script will be on my ec2 instance and will be triggered from there. How do I provide the connection to the database and the run the bunch of sql scripts ? Please guide
The easiest way to do this is to install postgres on the ec2 instance and then use the psql command line tools to connect to Redshift.
Install postgres (yum and apt-get, pick the appropriate one for your Linux type):
sudo apt-get install postgresql postgresql-contrib
sudo yum install postgresql postgresql-contrib
Here's a line from one of my bash scripts that does this. Check out the psql man page for all the options and ways to connect.
PGPASSWORD="$RS_password" psql -a -v ON_ERROR_STOP=1 -h "$RS_DNS" -v schema=public -p "$RS_port_no" -U "$RS_user" -d "$RS_db" -f /tmp/math_$$.SQL || { echo psql command 1 failed ; exit 1; }
There is also a -c option to pass the SQL on the command line.

2 Postgres Servers on local machine. How to uninstall or disassociate one?

Through some mishap I seem to have 2 separate Postgres servers on my local Ubuntu machine. PGAdmin connects to one (localhost:5432), which can also be reached with the terminal through: sudo -u postgres psql postgres -W -h 127.0.0.1 -p 5432
The other "default" server can be reached through the terminal with just sudo -u postgres psql postgres
For other info, locate bin/psql returns:
/usr/bin/psql
/usr/lib/postgresql/9.5/bin/psql
I suspect the latter of those directories is the Postgres 9.5 instance I really want to use, and the one I want to associate with without needing to specify -h 127.0.0.1 -p 5432. The other can be removed completely. How do I resolve this?
You can use the package manager to remove it.
From terminal, you can list the installed versions of PostgreSQL executing:
$ apt list --installed | grep postgresql
The ones with the format postgresql-X (where "X" is the version) is those to remove. You should be able to do it with the apt purge command.
For example, to remove the version 9.6, you have to execute:
$ sudo apt purge postgresql-9.6
/usr/bin/psql should be a symbolic link to the most recent version installed (/usr/lib/postgresql/9.6/bin/psql).

How to upgrade pg_dumpall

I am trying to migrate from Postgres 9.3 to 9.4, and I would like to run:
pg_dumpall > outfile.sql
But, I keep getting this error:
server version: 9.3.10; pg_dumpall version: 9.2.13
aborting because of server version mismatch
I found pg_dumpall here: /usr/bin/pg_dumpall
I found an answer saying to do this:
sudo ln -s /usr/lib/postgresql/9.3/bin/pg_dump /usr/bin/pg_dump --force
But in Centos, this is the output for this directory:
sudo ls /var/lib/pgsql/9.3
Output:
backups data initdb.log
Does anyone know how to upgrade pg_dumpall to match the postgres version in Centos 7?
CentOS has its own disk layout. I'd expect your 9.3 pg_dump will be in:
/usr/pgsql-9.3/bin/pg_dump
You can look for pg_dump files on your filesystem with:
find / -name pg_dump -executable -print

Postgresql -bash: psql: command not found

I have installed PostgreSQL and it is working ok. However, when I went to restore a backup I got the error -bash: psql: command not found:
[root#server1 ~]# su postgres
[postgres#server1 root]$ psql -f all.sql
bash: psql: command not found
[postgres#server1 root]$
What have I done wrong?
export PATH=/usr/pgsql-9.2/bin:$PATH
The program executable psql is in the directory /usr/pgsql-9.2/bin, and that directory is not included in the path by default, so we have to tell our shell (terminal) program where to find psql. When most packages are installed, they are added to an existing path, such as /usr/local/bin, but not this program.
So we have to add the program's path to the shell PATH variable if we do not want to have to type the complete path to the program every time we execute it.
This line should typically be added to theshell startup script, which for the bash shell will be in the file ~/.bashrc.
perhaps psql isn't in the PATH of the postgres user. Use the locate command to find where psql is and ensure that it's path is in the PATH for the postgres user.
The question is for linux but I had the same issue with git bash on my Windows machine.
My pqsql is installed here:
C:\Program Files\PostgreSQL\10\bin\psql.exe
You can add the location of psql.exe to your Path environment variable as
described in this other answer, and shown in the screenshot below:
After changing the above, please close all cmd and/or bash windows, and re-open them (as mentioned in the comments #Ayush Shankar). If you are using an IDE like Visual Studio Code, please close and re-open the entire IDE (as mentioned in the comments #Somraj Chowdhury)
You might need to change default logging user using below command.
psql -U postgres
Here postgres is the username. Without -U, it will pick the windows loggedin user.
It can be due to psql not being in PATH
$ locate psql
/usr/lib/postgresql/9.6/bin/psql
Then create a link in /usr/bin
ln -s /usr/lib/postgresql/9.6/bin/psql /usr/bin/psql
Then try to execute psql it should work.
In case you are running it on Fedora or CentOS, this is what worked for me (PostgreSQL 9.6):
In terminal:
$ sudo visudo -f /etc/sudoers
modify the following text from:
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin
to
Defaults secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/pgsql-9.6/bin
exit, then:
$ printenv PATH
$ sudo su postgres
$ psql
To exit postgreSQL terminal, you need to digit:
$ \q
Source: https://serverfault.com/questions/541847/why-doesnt-sudo-know-where-psql-is#comment623883_541880
If you are using the Postgres Mac app (by Heroku) and Bundler, you can add the pg_config directly inside the app, to your bundle.
bundle config build.pg --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config
...then run bundle again.
Note: check the version first using the following.
ls /Applications/Postgres.app/Contents/Versions/
Check if PostgreSQL is installed or not.
If not you can do the same in ubuntu using this command.
sudo apt update
sudo apt install postgresql postgresql-contrib
there must be two reasons for this either the package is not install or psql is not defined in the PATH
the simple way is to create a link within the /usr/bin or /usr/local/sbin/
First find the the file
sudo find / -name psql
then create soft link
sudo ln -sf /opt/pgpro/1c-14/bin/psql /usr/local/sbin/psql
Sometimes we face this issue when the gem installation command doesn't find the pg client library for various reasons, such as if psql is not in the path.
In those cases, providing the command with the path to pg_config may fix the issue.
gem install pg -v 1.3.5 -- --with-pg-config=/path/to/pg_config
In my case, I faced a similar issue when I installed postgresql#12 with Homebrew in the Rosetta environment.
Following command solved the issue in my case.
gem install pg -v 1.3.5 -- --with-pg-config=/usr/local/Homebrew/Cellar/postgresql#12/12.13/bin/pg_config

Resources