I have a Postgres 9.5 server with 30 databases.
I would like to find out which database uses which extension(s) in one go, preferably in a query that is usable in pgAdmin.
I know that SELECT * FROM pg_extension
gives me the extensions for the database I am connected to.
I also know that SELECT * FROM pg_available_extensions
lists, well, all available extensions.
I also know that SELECT * FROM pg_database
lists all existing databases.
Question
How can I create a list that includes the database name and the extensions used in each database?
-
Possibly the query on the following page can be adapted to get the data you seek: tapoueh.org/blog/2019/11/…cedricdlb– cedricdlb2022年01月28日 01:36:42 +00:00Commented Jan 28, 2022 at 1:36
2 Answers 2
You connect to each database in turn and query pg_extension
.
-
Is there away to do this in an automated way? like a script to give to psql or somethingpablete– pablete2023年06月05日 16:54:52 +00:00Commented Jun 5, 2023 at 16:54
I'm not sure if this can be automated directly in psql
, as AFAIK you can't do cross-database queries.
But you can easily automate this with some Bash. Here is quick script, which will list all databases in all PostgreSQL versions (clusters) and their extensions. Output is in Markdown format, so it is easier to share/store if needed.
echo "# Extensions in PostgreSQL databases"
while IFS= read -r cluster; do
echo "## ${cluster}"
while IFS= read -r dbname; do
echo "### ${dbname}"
psql --cluster="${cluster}" -U postgres -qAtc "SELECT CONCAT('- ', extname, ' ', extversion) FROM pg_extension;" "${dbname}"
echo
done < <(psql --cluster="${cluster}" -U postgres -qAtc "SELECT datname FROM pg_database WHERE datname != 'template0';")
echo
done < <(pg_lsclusters --no-header | awk '{ print 1ドル "/" 2ドル }')
Output looks like this:
## 9.5/main
### template1
- plpgsql 1.0
### postgres
- plpgsql 1.0
### somedb
- plpgsql 1.0
## 14/main
### postgres
- plpgsql 1.0
### template1
- plpgsql 1.0
### gitlab
- plpgsql 1.0
- btree_gist 1.6
- pg_trgm 1.6
-
Cross-db queries are possible, but painful.Gert van den Berg– Gert van den Berg2025年01月29日 12:01:59 +00:00Commented Jan 29 at 12:01
Explore related questions
See similar questions with these tags.