Searching a whole postgres db for a string












2















In phpmyadmin, I can easily search for text in the tables in my databases using the search tab. Was wondering if I can do the same with the postgres installed locally on my computer. I have looked at the menus there but I can't find anything like that. Does anyone know how I can search a whole db for a text that I enter?










share|improve this question


















  • 1





    You're comparing 2 different things: phpMyAdmin is not a DB, it's a PHP application written to administrate MySQL DBs, while PostgreSQL is a DB, like MySQL.

    – m0skit0
    Feb 17 '12 at 9:52











  • When you download postgress and install, pgadmin is also installed

    – yankitwizzy
    Feb 17 '12 at 13:01
















2















In phpmyadmin, I can easily search for text in the tables in my databases using the search tab. Was wondering if I can do the same with the postgres installed locally on my computer. I have looked at the menus there but I can't find anything like that. Does anyone know how I can search a whole db for a text that I enter?










share|improve this question


















  • 1





    You're comparing 2 different things: phpMyAdmin is not a DB, it's a PHP application written to administrate MySQL DBs, while PostgreSQL is a DB, like MySQL.

    – m0skit0
    Feb 17 '12 at 9:52











  • When you download postgress and install, pgadmin is also installed

    – yankitwizzy
    Feb 17 '12 at 13:01














2












2








2


1






In phpmyadmin, I can easily search for text in the tables in my databases using the search tab. Was wondering if I can do the same with the postgres installed locally on my computer. I have looked at the menus there but I can't find anything like that. Does anyone know how I can search a whole db for a text that I enter?










share|improve this question














In phpmyadmin, I can easily search for text in the tables in my databases using the search tab. Was wondering if I can do the same with the postgres installed locally on my computer. I have looked at the menus there but I can't find anything like that. Does anyone know how I can search a whole db for a text that I enter?







postgresql pgadmin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Feb 17 '12 at 8:54









yankitwizzyyankitwizzy

174239




174239








  • 1





    You're comparing 2 different things: phpMyAdmin is not a DB, it's a PHP application written to administrate MySQL DBs, while PostgreSQL is a DB, like MySQL.

    – m0skit0
    Feb 17 '12 at 9:52











  • When you download postgress and install, pgadmin is also installed

    – yankitwizzy
    Feb 17 '12 at 13:01














  • 1





    You're comparing 2 different things: phpMyAdmin is not a DB, it's a PHP application written to administrate MySQL DBs, while PostgreSQL is a DB, like MySQL.

    – m0skit0
    Feb 17 '12 at 9:52











  • When you download postgress and install, pgadmin is also installed

    – yankitwizzy
    Feb 17 '12 at 13:01








1




1





You're comparing 2 different things: phpMyAdmin is not a DB, it's a PHP application written to administrate MySQL DBs, while PostgreSQL is a DB, like MySQL.

– m0skit0
Feb 17 '12 at 9:52





You're comparing 2 different things: phpMyAdmin is not a DB, it's a PHP application written to administrate MySQL DBs, while PostgreSQL is a DB, like MySQL.

– m0skit0
Feb 17 '12 at 9:52













When you download postgress and install, pgadmin is also installed

– yankitwizzy
Feb 17 '12 at 13:01





When you download postgress and install, pgadmin is also installed

– yankitwizzy
Feb 17 '12 at 13:01










2 Answers
2






active

oldest

votes


















6














You could probably create a procedure to this, but out of the top of my head I'd just do a database dump (pg_dump) and search the file.






share|improve this answer
























  • Nice. I was going to suggest finding all textual columns with the System Catalogs but I like your answer better (for smallish databases)

    – Jared Beck
    May 28 '13 at 22:03











  • And for massive databases?

    – coderama
    Jun 14 '13 at 13:09











  • If it's a one time thing searching a dump from a backup is probably still the easiest thing. If you need it more ofter, use something like Sphinx or elasticsearch.

    – Elmar Weber
    Jun 18 '13 at 13:07











  • it's a 'silly' answer but the one that actually works :) So, is there someone who could post a proper SQL-Query?

    – three
    Nov 26 '13 at 7:16



















0














-- Below function will list all the tables which contain a specific string in the database 

select TablesCount(‘StringToSearch’);


--Iterates through all the tables in the database

CREATE OR REPLACE FUNCTION **TablesCount**(_searchText TEXT)
RETURNS text AS
$$ -- here start procedural part
DECLARE _tname text;
DECLARE cnt int;
BEGIN
FOR _tname IN SELECT table_name FROM information_schema.tables where table_schema='public' and table_type='BASE TABLE' LOOP
cnt= getMatchingCount(_tname,Columnames(_tname,_searchText));
RAISE NOTICE 'Count% ', CONCAT(' ',cnt,' Table name: ', _tname);
END LOOP;
RETURN _tname;
END;
$$ -- here finish procedural part
LANGUAGE plpgsql; -- language specification

-- Returns the count of tables for which the condition is met.
-- For example, if the intended text exists in any of the fields of the table,
-- then the count will be greater than 0. We can find the notifications
-- in the Messages section of the result viewer in postgres database.

CREATE OR REPLACE FUNCTION **getMatchingCount**(_tname TEXT, _clause TEXT)
RETURNS int AS
$$
Declare outpt text;
BEGIN
EXECUTE 'Select Count(*) from '||_tname||' where '|| _clause
INTO outpt;
RETURN outpt;
END;
$$ LANGUAGE plpgsql;


--Get the fields of each table. Builds the where clause with all columns of a table.
CREATE OR REPLACE FUNCTION **Columnames**(_tname text,st text)
RETURNS text AS
$$ -- here start procedural part
DECLARE
_name text;
_helper text;
BEGIN
FOR _name IN SELECT column_name FROM information_schema.Columns WHERE table_name =_tname LOOP
_name=CONCAT('CAST(',_name,' as VarChar)',' like ','''%',st,'%''', ' OR ');
_helper= CONCAT(_helper,_name,' ');
END LOOP;
RETURN CONCAT(_helper, ' 1=2');

END;
$$ -- here finish procedural part
LANGUAGE plpgsql; -- language specification





share|improve this answer

























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "3"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f390979%2fsearching-a-whole-postgres-db-for-a-string%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    6














    You could probably create a procedure to this, but out of the top of my head I'd just do a database dump (pg_dump) and search the file.






    share|improve this answer
























    • Nice. I was going to suggest finding all textual columns with the System Catalogs but I like your answer better (for smallish databases)

      – Jared Beck
      May 28 '13 at 22:03











    • And for massive databases?

      – coderama
      Jun 14 '13 at 13:09











    • If it's a one time thing searching a dump from a backup is probably still the easiest thing. If you need it more ofter, use something like Sphinx or elasticsearch.

      – Elmar Weber
      Jun 18 '13 at 13:07











    • it's a 'silly' answer but the one that actually works :) So, is there someone who could post a proper SQL-Query?

      – three
      Nov 26 '13 at 7:16
















    6














    You could probably create a procedure to this, but out of the top of my head I'd just do a database dump (pg_dump) and search the file.






    share|improve this answer
























    • Nice. I was going to suggest finding all textual columns with the System Catalogs but I like your answer better (for smallish databases)

      – Jared Beck
      May 28 '13 at 22:03











    • And for massive databases?

      – coderama
      Jun 14 '13 at 13:09











    • If it's a one time thing searching a dump from a backup is probably still the easiest thing. If you need it more ofter, use something like Sphinx or elasticsearch.

      – Elmar Weber
      Jun 18 '13 at 13:07











    • it's a 'silly' answer but the one that actually works :) So, is there someone who could post a proper SQL-Query?

      – three
      Nov 26 '13 at 7:16














    6












    6








    6







    You could probably create a procedure to this, but out of the top of my head I'd just do a database dump (pg_dump) and search the file.






    share|improve this answer













    You could probably create a procedure to this, but out of the top of my head I'd just do a database dump (pg_dump) and search the file.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Feb 17 '12 at 10:40









    Elmar WeberElmar Weber

    3161311




    3161311













    • Nice. I was going to suggest finding all textual columns with the System Catalogs but I like your answer better (for smallish databases)

      – Jared Beck
      May 28 '13 at 22:03











    • And for massive databases?

      – coderama
      Jun 14 '13 at 13:09











    • If it's a one time thing searching a dump from a backup is probably still the easiest thing. If you need it more ofter, use something like Sphinx or elasticsearch.

      – Elmar Weber
      Jun 18 '13 at 13:07











    • it's a 'silly' answer but the one that actually works :) So, is there someone who could post a proper SQL-Query?

      – three
      Nov 26 '13 at 7:16



















    • Nice. I was going to suggest finding all textual columns with the System Catalogs but I like your answer better (for smallish databases)

      – Jared Beck
      May 28 '13 at 22:03











    • And for massive databases?

      – coderama
      Jun 14 '13 at 13:09











    • If it's a one time thing searching a dump from a backup is probably still the easiest thing. If you need it more ofter, use something like Sphinx or elasticsearch.

      – Elmar Weber
      Jun 18 '13 at 13:07











    • it's a 'silly' answer but the one that actually works :) So, is there someone who could post a proper SQL-Query?

      – three
      Nov 26 '13 at 7:16

















    Nice. I was going to suggest finding all textual columns with the System Catalogs but I like your answer better (for smallish databases)

    – Jared Beck
    May 28 '13 at 22:03





    Nice. I was going to suggest finding all textual columns with the System Catalogs but I like your answer better (for smallish databases)

    – Jared Beck
    May 28 '13 at 22:03













    And for massive databases?

    – coderama
    Jun 14 '13 at 13:09





    And for massive databases?

    – coderama
    Jun 14 '13 at 13:09













    If it's a one time thing searching a dump from a backup is probably still the easiest thing. If you need it more ofter, use something like Sphinx or elasticsearch.

    – Elmar Weber
    Jun 18 '13 at 13:07





    If it's a one time thing searching a dump from a backup is probably still the easiest thing. If you need it more ofter, use something like Sphinx or elasticsearch.

    – Elmar Weber
    Jun 18 '13 at 13:07













    it's a 'silly' answer but the one that actually works :) So, is there someone who could post a proper SQL-Query?

    – three
    Nov 26 '13 at 7:16





    it's a 'silly' answer but the one that actually works :) So, is there someone who could post a proper SQL-Query?

    – three
    Nov 26 '13 at 7:16













    0














    -- Below function will list all the tables which contain a specific string in the database 

    select TablesCount(‘StringToSearch’);


    --Iterates through all the tables in the database

    CREATE OR REPLACE FUNCTION **TablesCount**(_searchText TEXT)
    RETURNS text AS
    $$ -- here start procedural part
    DECLARE _tname text;
    DECLARE cnt int;
    BEGIN
    FOR _tname IN SELECT table_name FROM information_schema.tables where table_schema='public' and table_type='BASE TABLE' LOOP
    cnt= getMatchingCount(_tname,Columnames(_tname,_searchText));
    RAISE NOTICE 'Count% ', CONCAT(' ',cnt,' Table name: ', _tname);
    END LOOP;
    RETURN _tname;
    END;
    $$ -- here finish procedural part
    LANGUAGE plpgsql; -- language specification

    -- Returns the count of tables for which the condition is met.
    -- For example, if the intended text exists in any of the fields of the table,
    -- then the count will be greater than 0. We can find the notifications
    -- in the Messages section of the result viewer in postgres database.

    CREATE OR REPLACE FUNCTION **getMatchingCount**(_tname TEXT, _clause TEXT)
    RETURNS int AS
    $$
    Declare outpt text;
    BEGIN
    EXECUTE 'Select Count(*) from '||_tname||' where '|| _clause
    INTO outpt;
    RETURN outpt;
    END;
    $$ LANGUAGE plpgsql;


    --Get the fields of each table. Builds the where clause with all columns of a table.
    CREATE OR REPLACE FUNCTION **Columnames**(_tname text,st text)
    RETURNS text AS
    $$ -- here start procedural part
    DECLARE
    _name text;
    _helper text;
    BEGIN
    FOR _name IN SELECT column_name FROM information_schema.Columns WHERE table_name =_tname LOOP
    _name=CONCAT('CAST(',_name,' as VarChar)',' like ','''%',st,'%''', ' OR ');
    _helper= CONCAT(_helper,_name,' ');
    END LOOP;
    RETURN CONCAT(_helper, ' 1=2');

    END;
    $$ -- here finish procedural part
    LANGUAGE plpgsql; -- language specification





    share|improve this answer






























      0














      -- Below function will list all the tables which contain a specific string in the database 

      select TablesCount(‘StringToSearch’);


      --Iterates through all the tables in the database

      CREATE OR REPLACE FUNCTION **TablesCount**(_searchText TEXT)
      RETURNS text AS
      $$ -- here start procedural part
      DECLARE _tname text;
      DECLARE cnt int;
      BEGIN
      FOR _tname IN SELECT table_name FROM information_schema.tables where table_schema='public' and table_type='BASE TABLE' LOOP
      cnt= getMatchingCount(_tname,Columnames(_tname,_searchText));
      RAISE NOTICE 'Count% ', CONCAT(' ',cnt,' Table name: ', _tname);
      END LOOP;
      RETURN _tname;
      END;
      $$ -- here finish procedural part
      LANGUAGE plpgsql; -- language specification

      -- Returns the count of tables for which the condition is met.
      -- For example, if the intended text exists in any of the fields of the table,
      -- then the count will be greater than 0. We can find the notifications
      -- in the Messages section of the result viewer in postgres database.

      CREATE OR REPLACE FUNCTION **getMatchingCount**(_tname TEXT, _clause TEXT)
      RETURNS int AS
      $$
      Declare outpt text;
      BEGIN
      EXECUTE 'Select Count(*) from '||_tname||' where '|| _clause
      INTO outpt;
      RETURN outpt;
      END;
      $$ LANGUAGE plpgsql;


      --Get the fields of each table. Builds the where clause with all columns of a table.
      CREATE OR REPLACE FUNCTION **Columnames**(_tname text,st text)
      RETURNS text AS
      $$ -- here start procedural part
      DECLARE
      _name text;
      _helper text;
      BEGIN
      FOR _name IN SELECT column_name FROM information_schema.Columns WHERE table_name =_tname LOOP
      _name=CONCAT('CAST(',_name,' as VarChar)',' like ','''%',st,'%''', ' OR ');
      _helper= CONCAT(_helper,_name,' ');
      END LOOP;
      RETURN CONCAT(_helper, ' 1=2');

      END;
      $$ -- here finish procedural part
      LANGUAGE plpgsql; -- language specification





      share|improve this answer




























        0












        0








        0







        -- Below function will list all the tables which contain a specific string in the database 

        select TablesCount(‘StringToSearch’);


        --Iterates through all the tables in the database

        CREATE OR REPLACE FUNCTION **TablesCount**(_searchText TEXT)
        RETURNS text AS
        $$ -- here start procedural part
        DECLARE _tname text;
        DECLARE cnt int;
        BEGIN
        FOR _tname IN SELECT table_name FROM information_schema.tables where table_schema='public' and table_type='BASE TABLE' LOOP
        cnt= getMatchingCount(_tname,Columnames(_tname,_searchText));
        RAISE NOTICE 'Count% ', CONCAT(' ',cnt,' Table name: ', _tname);
        END LOOP;
        RETURN _tname;
        END;
        $$ -- here finish procedural part
        LANGUAGE plpgsql; -- language specification

        -- Returns the count of tables for which the condition is met.
        -- For example, if the intended text exists in any of the fields of the table,
        -- then the count will be greater than 0. We can find the notifications
        -- in the Messages section of the result viewer in postgres database.

        CREATE OR REPLACE FUNCTION **getMatchingCount**(_tname TEXT, _clause TEXT)
        RETURNS int AS
        $$
        Declare outpt text;
        BEGIN
        EXECUTE 'Select Count(*) from '||_tname||' where '|| _clause
        INTO outpt;
        RETURN outpt;
        END;
        $$ LANGUAGE plpgsql;


        --Get the fields of each table. Builds the where clause with all columns of a table.
        CREATE OR REPLACE FUNCTION **Columnames**(_tname text,st text)
        RETURNS text AS
        $$ -- here start procedural part
        DECLARE
        _name text;
        _helper text;
        BEGIN
        FOR _name IN SELECT column_name FROM information_schema.Columns WHERE table_name =_tname LOOP
        _name=CONCAT('CAST(',_name,' as VarChar)',' like ','''%',st,'%''', ' OR ');
        _helper= CONCAT(_helper,_name,' ');
        END LOOP;
        RETURN CONCAT(_helper, ' 1=2');

        END;
        $$ -- here finish procedural part
        LANGUAGE plpgsql; -- language specification





        share|improve this answer















        -- Below function will list all the tables which contain a specific string in the database 

        select TablesCount(‘StringToSearch’);


        --Iterates through all the tables in the database

        CREATE OR REPLACE FUNCTION **TablesCount**(_searchText TEXT)
        RETURNS text AS
        $$ -- here start procedural part
        DECLARE _tname text;
        DECLARE cnt int;
        BEGIN
        FOR _tname IN SELECT table_name FROM information_schema.tables where table_schema='public' and table_type='BASE TABLE' LOOP
        cnt= getMatchingCount(_tname,Columnames(_tname,_searchText));
        RAISE NOTICE 'Count% ', CONCAT(' ',cnt,' Table name: ', _tname);
        END LOOP;
        RETURN _tname;
        END;
        $$ -- here finish procedural part
        LANGUAGE plpgsql; -- language specification

        -- Returns the count of tables for which the condition is met.
        -- For example, if the intended text exists in any of the fields of the table,
        -- then the count will be greater than 0. We can find the notifications
        -- in the Messages section of the result viewer in postgres database.

        CREATE OR REPLACE FUNCTION **getMatchingCount**(_tname TEXT, _clause TEXT)
        RETURNS int AS
        $$
        Declare outpt text;
        BEGIN
        EXECUTE 'Select Count(*) from '||_tname||' where '|| _clause
        INTO outpt;
        RETURN outpt;
        END;
        $$ LANGUAGE plpgsql;


        --Get the fields of each table. Builds the where clause with all columns of a table.
        CREATE OR REPLACE FUNCTION **Columnames**(_tname text,st text)
        RETURNS text AS
        $$ -- here start procedural part
        DECLARE
        _name text;
        _helper text;
        BEGIN
        FOR _name IN SELECT column_name FROM information_schema.Columns WHERE table_name =_tname LOOP
        _name=CONCAT('CAST(',_name,' as VarChar)',' like ','''%',st,'%''', ' OR ');
        _helper= CONCAT(_helper,_name,' ');
        END LOOP;
        RETURN CONCAT(_helper, ' 1=2');

        END;
        $$ -- here finish procedural part
        LANGUAGE plpgsql; -- language specification






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 5 at 8:15

























        answered Feb 5 at 6:46









        GaneshGanesh

        11




        11






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Super User!


            • 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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f390979%2fsearching-a-whole-postgres-db-for-a-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

            Eduardo VII do Reino Unido