且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何搜索所有表(PostgreSQL)中的特定值?

更新时间:2023-12-01 10:06:04

如何转储数据库内容,然后使用 grep

How about dumping the contents of the database, then using grep?

$ pg_dump --data-only --inserts -U postgres your-db-name > a.tmp
$ grep United a.tmp
INSERT INTO countries VALUES ('US', 'United States');
INSERT INTO countries VALUES ('GB', 'United Kingdom');

相同的实用程序pg_dump可以在输出中包含列名。只需更改 - 插入 - 列插入。这样您也可以搜索特定的列名称。但如果我在寻找列名,我可能会转储模式而不是数据。

The same utility, pg_dump, can include column names in the output. Just change --inserts to --column-inserts. That way you can search for specific column names, too. But if I were looking for column names, I'd probably dump the schema instead of the data.

$ pg_dump --data-only --column-inserts -U postgres your-db-name > a.tmp
$ grep country_code a.tmp
INSERT INTO countries (iso_country_code, iso_country_name) VALUES ('US', 'United  States');
INSERT INTO countries (iso_country_code, iso_country_name) VALUES ('GB', 'United Kingdom');