Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Rails ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "pg_range" does not exist LINE 3: LEFT JOIN pg_range as r ON oid = rngtypid

Ask Question

I'm using an existing Postgres database for a rails 6 project. When I do some ActiveRecord queries like User.all() , I run into the following error

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "pg_range" does not exist
LINE 3: LEFT JOIN pg_range as r ON oid = rngtypid

After some googling. I found that pg_range was introduced in PostgreSQL 9.2. My existing database was upgraded from an older version (8.x, if I remembered correctly). Was that the reason I ran into this issue. How can I fix it?

Any PostgreSQL database of version 9.2 or above will always have a pg_catalog.pg_range table, be it upgraded from an older version or not.

There are two possible explanations for your error:

  • You are inadvertently working with a PostgreSQL database of version 9.0 or lower.

  • You somehow managed to corrupt the system catalogs by dropping pg_range. There are safeguards against that, so that's nothing that can happen by mistake.

  • Hi @Laurenz, thank you for your answer. I checked my psql version and it is 10.6 (psql (PostgreSQL) 10.6 (Ubuntu 10.6-0ubuntu0.18.04.1)). I didn't remember dropping pg_range myself. But it's possible that I accidentally corrupt it when upgrading system version. Any idea how I can recover it? – Chris.Zou Sep 3, 2019 at 6:30 What you are showing is the client version. What is the result of the SQL statement SHOW server_version;? – Laurenz Albe Sep 3, 2019 at 6:40 Hi @Laurenz, I ran SHOW server_version; as you said in my psql shell and it was 9.1.24 😂I assume this was the cause of the issue? Thank you again! – Chris.Zou Sep 3, 2019 at 6:49 Yes. That is very old and outdated software, and you shouldn't use it if you love your data. – Laurenz Albe Sep 3, 2019 at 14:40

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.