pg_stat_statements is in the config but the view cannot be queried
ERROR: relation "pg_stat_statements" does not existShort answer
pg_stat_statements needs two separate things. The server must load the module through shared_preload_libraries and be restarted, and each database where you query it needs CREATE EXTENSION pg_stat_statements. Query identifiers must be enabled too. Missing either step gives a missing view or an unloaded module.
Fix it fast
1Find out which cause you have
Check what the running server has loaded, and whether the extension exists in the database you are connected to.
SHOW shared_preload_libraries; \\dx pg_stat_statements2Match what you see, then jump to its fix
- 1
SHOW shared_preload_libraries does not list pg_stat_statementsThe module is not preloaded, or the server was not restartedFix - 2
The module is loaded but the view is missing in one database and present in anotherThe extension was never created in this databaseFix - 3
The view exists but stays empty while queries runQuery identifiers are turned offFix
- 1
What does the error look like?
ERROR: relation "pg_stat_statements" does not exist
Cause 1 of 3
The module is not preloaded, or the server was not restarted
1Check
The module needs extra shared memory, so it must be named in shared_preload_libraries and the server restarted. Reloading the config is not enough.
SHOW shared_preload_libraries;2Fix
Add it in postgresql.conf, then restart the server.
# postgresql.conf
shared_preload_libraries = 'pg_stat_statements'
sudo systemctl restart postgresql3Verify
The setting now lists the module.
SHOW shared_preload_libraries;Confirmed by postgresql.org
Cause 2 of 3
The extension was never created in this database
1Check
Statistics are collected across all databases, but the view and functions exist only in databases where the extension was created.
\c mydb
\dx2Fix
Create the extension in each database where you want to query it.
CREATE EXTENSION pg_stat_statements;3Verify
The view answers.
SELECT count(*) FROM pg_stat_statements;Confirmed by postgresql.org
Cause 3 of 3
Query identifiers are turned off
1Check
The module is active only when query identifier calculation is enabled, which happens automatically when compute_query_id is auto or on.
SHOW compute_query_id;2Fix
Set compute_query_id to auto or on in postgresql.conf and apply the change.
# postgresql.conf
compute_query_id = on
# then reload the configuration3Verify
Run a few queries and read the view.
SELECT query, calls FROM pg_stat_statements ORDER BY calls DESC LIMIT 5;Confirmed by postgresql.org
How do I stop it coming back?
- Put the preload line in the configuration management, not only in a manual edit.
- Create the extension in the template database if every new database needs it.
- After editing shared_preload_libraries, always restart; a reload does nothing for it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Databases
DBeaver cannot run pg_restore: the utility is not found in the client home
Utility 'pg_restore' not found in client home '/usr/lib' (/usr/lib)
DBeaver runs pg_restore from a local PostgreSQL client folder; it does not bundle one.
3 causes, each with a fixDatabases
DBeaver cannot download the PostgreSQL driver from Maven
The PostgreSQL driver files are missing and the download does not complete
DBeaver downloads the PostgreSQL JDBC driver from a Maven repository the first time you connect.
3 causes, each with a fixDatabases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint
A model field is NOT NULL in the database unless it has null=True.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 causes, each with a fixDatabases
Django cannot connect to PostgreSQL: connection refused on port 5432, locally or in Docker
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the host and port Django used.
4 causes, each with a fix