How to fix "ERROR: column c.relhasoids does not exist" in Postgres?

Solution 1:

I am able to reproduce your error if I am using Postgres v.12 and an older client (v.11 or earlier):

[root@def /]# psql -h 172.17.0.3
psql (11.5, server 12.0)
WARNING: psql major version 11, server major version 12.
         Some psql features might not work.
Type "help" for help.

postgres=# create table mytable (id int, name text);
CREATE TABLE
postgres=# table mytable;
 id | name 
----+------
(0 rows)

postgres=# \d mytable;
ERROR:  column c.relhasoids does not exist
LINE 1: ...riggers, c.relrowsecurity, c.relforcerowsecurity, c.relhasoi...
                                                             ^
postgres=# 

This is because in v. 12, table OIDs are no longer treated as special columns, and hence the relhasoids column is no longer necessary. Please make sure you're using a v. 12 psql binary so you don't encounter this error.

You may not necessarily be using psql, so the more general answer here is to make sure you’re using a compatible client.

Solution 2:

For anyone running Postgres as a Docker container:

Instead of running psql from the host, run it from inside the container e.g.

docker exec -it postgres_container_name psql your_connection_string

The Postgres image always ships with the corresponding—and thus always updated—version of psql so you don't have to worry about having the correct version installed on the host machine.

Solution 3:

If you're using DataGrip, there's an easy fix:

Try using "Introspect using JDBC metadata". This fixed it for me when (I think) I had a version mismatch between postgresql server and DataGrip client.

Under your connection settings -> Options tab -> check Introspect using JDBC metadata

According to https://www.jetbrains.com/help/datagrip/data-sources-and-drivers-dialog.html#optionsTab :

Switch to the JDBC-based introspector.

To retrieve information about database objects (DB metadata), DataGrip uses the following introspectors:

  1. A native introspector (might be unavailable for certain DBMS). The native introspector uses DBMS-specific tables and views as a source of metadata. It can retrieve DBMS-specific details and produce a more precise picture of database objects.

  2. A JDBC-based introspector (available for all the DBMS). The JDBC-based introspector uses the metadata provided by the JDBC driver. It can retrieve only standard information about database objects and their properties.

Consider using the JDBC-based intorspector when the native introspector fails or is not available.

The native introspector can fail, when your database server version is older than the minimum version supported by DataGrip.

You can try to switch to the JDBC-based introspector to fix problems with retrieving the database structure information from your database. For example, when the schemas that exist in your database or database objects below the schema level are not shown in the Database tool window.