SQLAlchethe: table version already exists. pgAdmin4 fails on Linux Mint 20 and 20.1

pgadmin-4sqlalchemy

I already found the solution, I just want to share it in case someone has the same problem.

Situation

Install pgAdmin4 version 4.30 on Linux Mint 20 (Ulyana) and Linux Mint 20.1 (Ulyssa) inside virtualenv. The bulk of pgAdmin4 is a Python web application written using the Flask framework. Therefore, it is an ideal candidate to be implemented in a Python virtualenv as indicated in:

How to Install PostgreSQL with pgAdmin4 on Linux Mint 20

And,

Installing pgAdmin 4 on Linux Mint 20 (ulyana)

And in Spanish,

Cómo instalar PostgreSQL con pgAdmin4 en Linux Mint 20

Error

After finishing the process, when trying to run pgAdmin4 for the first time, the following error appears:

Traceback (most recent call last):
  File "venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.py", line 87, in <module>
    exec(open(file_quote(setup_py), 'r').read())
  File "<string>", line 449, in <module>
  File "<string>", line 372, in setup_db
  
  [...]

  File "/home/mint/pgAdmin4/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 575, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "/home/mint/pgAdmin4/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 908, in apply_driver_hacks
    sa_url.database = os.path.join(app.root_path, sa_url.database)
AttributeError: can't set attribute

In the second attempt to execute pgAdmin4 the error shown is:

Traceback (most recent call last):
  File "/home/mint/pgAdmin4/venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1705, in _execute_context
    self.dialect.do_execute(
  File "/home/mint/pgAdmin4/venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 681, in do_execute
    cursor.execute(statement, parameters)
sqlite3.OperationalError: table version already exists

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.py", line 94, in <module>
    app = create_app()
  
  [...]

  File "/home/mint/pgAdmin4/venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 681, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) table version already exists
[SQL: 
CREATE TABLE version (
    name VARCHAR(32) NOT NULL, 
    value INTEGER NOT NULL, 
    PRIMARY KEY (name)
)

]
(Background on this error at: http://sqlalche.me/e/14/e3q8)

This situation started to occur in March 2021 and as far as I know, it is identical on both Linux Mint 20 and Linux Mint 20.1.

Note: a more suitable location to put the virtualenv would have been /opt, but for multiple tests I opted for /home from a live USB with Linux Mint 20. Then apply the final changes to my Linux Mint 20.1

Best Answer

Solution

Downgrade SQLAlchemy to an earlier non-conflict version.

At the time of publishing the posts that explain the steps to install pgAdmin4 within virtualenv on Linux Mint 20, pip was implementing SQLAlchemy 1.3.23. Then, in these last days, the version used is SQLAlchemy 1.4.1

Downgrade can be done with pip install --upgrade. --upgrade can be used for both, downgrade or upgrade.

(venv) mint@mint:~/pgAdmin4$ pip install --upgrade SQLAlchemy==1.3.23

After this, when trying to run pdAdmin4, a new problem will be displayed:

Traceback (most recent call last):
  File "venv/lib/python3.8/site-packages/pgadmin4/pgAdmin4.py", line 94, in <module>
    app = create_app()
  File "/home/mint/pgAdmin4/venv/lib/python3.8/site-packages/pgadmin4/pgadmin/__init__.py", line 347, in create_app
    if not os.path.exists(SQLITE_PATH) or get_version() == -1:
  File "/home/mint/pgAdmin4/venv/lib/python3.8/site-packages/pgadmin4/pgadmin/setup/db_version.py", line 19, in get_version
    return version.value
AttributeError: 'NoneType' object has no attribute 'value'

This new issue is resolved by removing the ~/.pgadmin directory (created during installation) and then rerunning "python pgAdmin4.py"