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

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]#

Related

How to run the 'postgres' command (single-user)

I have postgres installed on an ubuntu machine, and I am able to enter into the command line via something along the lines of:
$ sudo -u postgres psql
psql (10.15 (Ubuntu 10.15-0ubuntu0.18.04.1))
Type "help" for help.
postgres=#
And I can start/stop the server by doing something like:
$ sudo service postgresql
Usage: /etc/init.d/postgresql {start|stop|restart|reload|force-reload|status} [version ..]
Those both seem fine. However, I would like to run postgres in single-user mode to do a couple tests. On the postgres page it gives a few examples, such as:
To start a single-user mode server, use a command like
postgres --single -D /usr/local/pgsql/data other-options my_database
However, if I use the 'postgres' command, I just get an error saying I don't have that command:
$ postgres
Command 'postgres' not found, did you mean:
What do I need to install to run the 'postgres' command in order to enter single-user mode?
as you have not export the binary path that's why it's can't find your binary of postgres.
use this command:
/usr/lib/postgresql/10/bin/postgres --single -D /usr/local/pgsql/data other-options my_database
or,
you can export the path in bash
first open the bashrc with this command:nano ~/.bashrc
add this line in the end :PATH="/usr/lib/postgresql/10/bin/:$PATH"
run this command source ~/.bashrc
the just use postgres --single -D /usr/local/pgsql/data other-options my_database
you can also find where your binary is with this command : find /usr/lib -iname 'postgres'
It is already installed, it is just not in your PATH, as it is not anticipated you would use it manually.
It is probably somewhere like "/usr/lib/postgresql/10/bin/postgres", or you can use locate or find to find it.
Ubuntu has conf files spread over several places so:
/usr/lib/postgresql/13/bin/postgres --single -D /var/lib/postgresql/13/main -c "config_file=/etc/postgresql/13/main/postgresql.conf"

Could not load library /usr/local/lib/postgresql/plpgsql.so .. undefined symbol "MakeExpandedObjectReadOnly"

What I'm trying to do is to convert this installing script for webodm (https://gist.github.com/lkpanganiban/5226cc8dd59cb39cdc1946259c3fea6e) written in bash to be used in tcsh shell under a freenas jail.
I have now enter at part where I can't find a solution to and my hope is that someone can en light me what to do next.
The line that is triggering the problem is :
su - postgres -c "psql -d webodm_dev -c "\""CREATE EXTENSION postgis;"\"" "
The whole error line :
ERROR: could not load library "/usr/local/lib/postgresql/plpgsql.so": dlopen (/usr/local/lib/postgresql/plpgsql.so) failed: /usr/local/lib/postgresql/plpgsql.so: Undefined symbol "MakeExpandedObjectReadOnly"
pkg info give :
postgis24-2.4.5_1 Geographic objects support for PostgreSQL databases
postgresql95-client-9.5.15_2 PostgreSQL database (client)
postgresql95-contrib-9.5.15_2 The contrib utilities from the PostgreSQL distribution
postgresql95-server-9.5.15_2 PostgreSQL is the most advanced open-source database available anywhere
And yes the file exists:
root#webodm2:~ # ls -l /usr/local/lib/postgresql/plpgsql.so
-rwxr-xr-x 1 root wheel 195119 Feb 7 18:16 /usr/local/lib/postgresql/plpgsql.so
root#webodm2:~ #
So anyone have some idea ?
I faced this issue after the upgrade from postgres 11 to 12, here how to fix it for Linux and Mac (without brew)
$ sudo su postgres
$ /usr/lib/postgresql/12/bin/pg_upgrade \
--old-datadir=/var/lib/postgresql/11/main \
--new-datadir=/var/lib/postgresql/12/main \
--old-bindir=/usr/lib/postgresql/11/bin \
--new-bindir=/usr/lib/postgresql/12/bin \
--old-options '-c config_file=/etc/postgresql/11/main/postgresql.conf' \
--new-options '-c config_file=/etc/postgresql/12/main/postgresql.conf' \
you can add --check to do a dry test upgrade without changing anything in your postgres installation.
for Mac users with brew installation:
after the upgrade run the following command"
$ brew postgresql-upgrade-database
That error message means that you have a plpgsql.so from PostgreSQL 9.5 or earlier and try to use it with PostgreSQL 9.6 or later.
Either you are picking up the wrong library, or you copied files around.
Anyway, the problem has nothing to do with PostGIS.
It might be your database has an outdated version, try to run the checks before running brew postgresql-upgrade-database. OR try to restart your service brew services restart postgres.
psql --version # 11.4 <--- psql cli version
psql -c 'select version();' postgres # 10.2 <--- db version in storage
brew info postgres # check pg info <--- found solution
brew postgresql-upgrade-database # upgrade db version in storage and fixed the issue

Why postgresql on mac asks me for password after fresh install?

I just installed postgresql on a Macbook with brew install postgresql. Then I try to psql, but it requires password and then show psql: FATAL: password authentication failed for user "<myname>".
I have not set up anything, and inputting my mac password does nothing. What should I do now?
So your username probably does not exist, as the default username that ships with the db is postgres.
Further, I was prevented from the submission of an empty password, which is blank by default for the postgres user.
You might try
cd ~/
sudo -u postgres psql -c "ALTER USER postgres PASSWORD 'postgres';"
Password: YOUR_LOGIN_PWD_HERE (required for sudo)
and then to use
psql -U postgres
password: postgres
I'm not 100% sure of which SO answer I got this from, perhaps here. Hope this helps.
The above did not work for me.
The below steps worked for me:
Step 1: Uninstall Postgres using the following steps:
sudo /Library/PostgreSQL/10/uninstall-postgresql.app/Contents/MacOS/uninstall-postgresql
PS: my postgres version is 10
Step 2: Remove Postgresql user
System Preference > userse & Groups > Unlock > remove postgresql user by clicking "-"
Step 3: Remove existing databases
rm -rf /usr/local/var/postgres/*
Step 4: Install and Start Postgres using brew
brew update
brew install postgresql
brew services start postgresql
Step 5: Create database
initdb /usr/local/var/postgres -E utf8
You can start accessing postgres
psql -h localhost -d postgres
The answer by Pramida almost worked for me... the difference is I was using 9.6 Postgres.
So I ran:
sudo
/Library/PostgreSQL/9.6/uninstall-postgresql.app/Contents/MacOS/installbuilder.sh
and somehow that got rid of my username and almost all of postgres user. I think
I then blew away the directory
sudo rm -rf /Library/PostgreSQL/9.6
And then I installed using brew above.
in my case macboook big sur v 11 you should create /var/postgresql#12 in Mackbook/usr/local
and open terminal in /opt/homebrew/Cellar/postgresql#12/12.8/bin and run
/opt/homebrew/Cellar/postgresql#12/12.8/bin/initdb -D /usr/local/var/postgresql#12
then run in terminal
echo 'export PATH="/opt/homebrew/Cellar/postgresql#12/12.8/bin:$PATH"' >> ~/.zshrc
then
psql -h localhost -p 5432 -d postgres
and enjoy creating user
rm -rf /usr/local/var/postgres && initdb /usr/local/var/postgres -E utf8

Linuxbrew, install postgres and start service automatically

I install Postgresql on my Ubuntu with:
brew install postgres
now I have:
psql --version
psql (PostgreSQL) 9.5.0
How can I start the service automatically?
On my Mac with homebrew I can do it with:
ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist
but how on Ubuntu with Linuxbrew?
I try with:
brew services start postgresql
but it says:
sh: 1: list: not found
Error: Could not read the plist for `postgresql`!
What to do?
Not quite automatic but a step in the right direction. On my system I did the following:
$ pg_ctl start -D $HOME/.linuxbrew/var/postgres -l logfile
You could simple create an alias in your .bash_profile or .bashrc something like:
alias poston="pg_ctl start -D $HOME/.linuxbrew/var/postgres -l logfile"
alias postoff="pg_ctl stop -D $HOME/.linuxbrew/var/postgres"
To test it all out (assuming you do not yet have a database on your machine) you can also create a database for your current user:
$ poston
$ createdb `whoami`
$ psql

How do I correct the mismatch of psql version and postgresql version?

Somewhere along the line I have ended up with a version mismatch between postgresql-9.4 and psql which is version 9.3 despite the fact that version 9.4 is installed.
I think I need to correct the $PATH variable but I don't know where to find this. I've looked in my /etc/.bashrc file and can't see anything that points me in the right direction.
When I do sudo find / -name psql the result is:
/usr/bin/psql
/usr/share/bash-completion/completions/psql
/usr/pgsql-9.4/bin/psql
yum list installed | grep postgres yields the following:
postgresql.x86_64 9.3.9-1.fc21 #updates
postgresql-contrib.x86_64 9.3.9-1.fc21 #updates
postgresql-devel.x86_64 9.3.9-1.fc21 #updates
postgresql-libs.x86_64 9.3.9-1.fc21 #updates
postgresql-server.x86_64 9.3.9-1.fc21 #updates
postgresql94.x86_64 9.4.5-1PGDG.f21 #pgdg94
postgresql94-libs.x86_64 9.4.5-1PGDG.f21 #pgdg94
postgresql94-server.x86_64 9.4.5-1PGDG.f21 #pgdg94
As Craig Ringer answered - generally you should use:
sudo update-alternatives --config pgsql-psql
Nevertheless, sometimes you can get such message back:
failed to link /usr/bin/psql -> /etc/alternatives/pgsql-psql: /usr/bin/psql exists and it is not a symlink
If so - try:
ln -s /usr/pgsql-[PUT YOUR VERSION HERE(ex. 9.6)]/bin/psql /usr/local/bin/psql
Use the alternatives mechanism. On Fedora:
sudo update-alternatives --config pgsql-psql
You're apparently using CentOS or RHEL.
In this case, you might: 1) invoke psql with the full path or 2) replace the current psql in the alternatives by executing the following command as root:
update-alternatives --install /usr/bin/psql pgsql-psql /usr/pgsql-9.4/bin/psql 1
A tip: use updatedb and locate instead of find whenever looking for a file or directory. :D