Python psycopg2 executing select pg_notify doesn't work











up vote
1
down vote

favorite












This is my very first question at StackOverflow so if i am doing something wrong please be gentle.



I'm struggling with executing SELECT pg_notify from Python script. It seems that it doesn't work at all.



My NodeJS server is listening 'testnotify' channel using pg-promise. I'm putting this just for completeness because it is working.



db.connect({direct: true})
.then(sco => {
sco.client.on('notification', data => {
console.log('Received:', data);
});
return sco.none('LISTEN $1~', 'testnotify');
})
.catch(error => {
console.log('Error:', error);
});


My Python script should rise notification after series of successful db operations.



I'm doing this like that



conn = psycopg2.connect(conn_string)
cur = conn.cursor()
cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


or like that



query = "SELECT pg_notify('testnotify', 'blabla');"
print(query)
cur.execute(query)


I've tried in similar way with NOTIFY testnotify, 'blabla' and nothing works. Nothing happen at NodeJS side.
But when i copy result of print(query) from Python console and execute it directly from PostgreSQL then it is working like a charm.
I don't understand what's wrong with my code.



I'm using psycopg2 2.7.5, PostgreSQL 10, Node 10 LTS, pg-promise at Windows 10.



Side note: This is not a problem with Node because it is working when pg_notify or notify is raised using trigger at source table in postgresql or when executing notification as regular sql query at db. It is not working only when i'm trying to raise notification from python script.



After two days of juggling with this i think that this is something obvious and stupid but i can't see it. Or maybe it is just impossible...



Please help.










share|improve this question


























    up vote
    1
    down vote

    favorite












    This is my very first question at StackOverflow so if i am doing something wrong please be gentle.



    I'm struggling with executing SELECT pg_notify from Python script. It seems that it doesn't work at all.



    My NodeJS server is listening 'testnotify' channel using pg-promise. I'm putting this just for completeness because it is working.



    db.connect({direct: true})
    .then(sco => {
    sco.client.on('notification', data => {
    console.log('Received:', data);
    });
    return sco.none('LISTEN $1~', 'testnotify');
    })
    .catch(error => {
    console.log('Error:', error);
    });


    My Python script should rise notification after series of successful db operations.



    I'm doing this like that



    conn = psycopg2.connect(conn_string)
    cur = conn.cursor()
    cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


    or like that



    query = "SELECT pg_notify('testnotify', 'blabla');"
    print(query)
    cur.execute(query)


    I've tried in similar way with NOTIFY testnotify, 'blabla' and nothing works. Nothing happen at NodeJS side.
    But when i copy result of print(query) from Python console and execute it directly from PostgreSQL then it is working like a charm.
    I don't understand what's wrong with my code.



    I'm using psycopg2 2.7.5, PostgreSQL 10, Node 10 LTS, pg-promise at Windows 10.



    Side note: This is not a problem with Node because it is working when pg_notify or notify is raised using trigger at source table in postgresql or when executing notification as regular sql query at db. It is not working only when i'm trying to raise notification from python script.



    After two days of juggling with this i think that this is something obvious and stupid but i can't see it. Or maybe it is just impossible...



    Please help.










    share|improve this question
























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      This is my very first question at StackOverflow so if i am doing something wrong please be gentle.



      I'm struggling with executing SELECT pg_notify from Python script. It seems that it doesn't work at all.



      My NodeJS server is listening 'testnotify' channel using pg-promise. I'm putting this just for completeness because it is working.



      db.connect({direct: true})
      .then(sco => {
      sco.client.on('notification', data => {
      console.log('Received:', data);
      });
      return sco.none('LISTEN $1~', 'testnotify');
      })
      .catch(error => {
      console.log('Error:', error);
      });


      My Python script should rise notification after series of successful db operations.



      I'm doing this like that



      conn = psycopg2.connect(conn_string)
      cur = conn.cursor()
      cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


      or like that



      query = "SELECT pg_notify('testnotify', 'blabla');"
      print(query)
      cur.execute(query)


      I've tried in similar way with NOTIFY testnotify, 'blabla' and nothing works. Nothing happen at NodeJS side.
      But when i copy result of print(query) from Python console and execute it directly from PostgreSQL then it is working like a charm.
      I don't understand what's wrong with my code.



      I'm using psycopg2 2.7.5, PostgreSQL 10, Node 10 LTS, pg-promise at Windows 10.



      Side note: This is not a problem with Node because it is working when pg_notify or notify is raised using trigger at source table in postgresql or when executing notification as regular sql query at db. It is not working only when i'm trying to raise notification from python script.



      After two days of juggling with this i think that this is something obvious and stupid but i can't see it. Or maybe it is just impossible...



      Please help.










      share|improve this question













      This is my very first question at StackOverflow so if i am doing something wrong please be gentle.



      I'm struggling with executing SELECT pg_notify from Python script. It seems that it doesn't work at all.



      My NodeJS server is listening 'testnotify' channel using pg-promise. I'm putting this just for completeness because it is working.



      db.connect({direct: true})
      .then(sco => {
      sco.client.on('notification', data => {
      console.log('Received:', data);
      });
      return sco.none('LISTEN $1~', 'testnotify');
      })
      .catch(error => {
      console.log('Error:', error);
      });


      My Python script should rise notification after series of successful db operations.



      I'm doing this like that



      conn = psycopg2.connect(conn_string)
      cur = conn.cursor()
      cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


      or like that



      query = "SELECT pg_notify('testnotify', 'blabla');"
      print(query)
      cur.execute(query)


      I've tried in similar way with NOTIFY testnotify, 'blabla' and nothing works. Nothing happen at NodeJS side.
      But when i copy result of print(query) from Python console and execute it directly from PostgreSQL then it is working like a charm.
      I don't understand what's wrong with my code.



      I'm using psycopg2 2.7.5, PostgreSQL 10, Node 10 LTS, pg-promise at Windows 10.



      Side note: This is not a problem with Node because it is working when pg_notify or notify is raised using trigger at source table in postgresql or when executing notification as regular sql query at db. It is not working only when i'm trying to raise notification from python script.



      After two days of juggling with this i think that this is something obvious and stupid but i can't see it. Or maybe it is just impossible...



      Please help.







      python-3.x psycopg2 postgresql-10






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 8 at 10:19









      Paabo

      62




      62
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Oh my... i just found solution... even two ways to solve this.



          The clue was in psycopg documentation... obvious huh?



          to send notification using



          cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


          one have to set connection to autocommit like that



          import psycopg2
          import psycopg2.extensions
          conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)


          or simply if you don't want autocommit then after doing



          cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


          you have to commit it



          conn.commit()


          aaand now Node is receiving notifications from postgresql via python






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            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',
            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%2fstackoverflow.com%2fquestions%2f53205675%2fpython-psycopg2-executing-select-pg-notify-doesnt-work%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Oh my... i just found solution... even two ways to solve this.



            The clue was in psycopg documentation... obvious huh?



            to send notification using



            cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


            one have to set connection to autocommit like that



            import psycopg2
            import psycopg2.extensions
            conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)


            or simply if you don't want autocommit then after doing



            cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


            you have to commit it



            conn.commit()


            aaand now Node is receiving notifications from postgresql via python






            share|improve this answer

























              up vote
              0
              down vote













              Oh my... i just found solution... even two ways to solve this.



              The clue was in psycopg documentation... obvious huh?



              to send notification using



              cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


              one have to set connection to autocommit like that



              import psycopg2
              import psycopg2.extensions
              conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)


              or simply if you don't want autocommit then after doing



              cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


              you have to commit it



              conn.commit()


              aaand now Node is receiving notifications from postgresql via python






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Oh my... i just found solution... even two ways to solve this.



                The clue was in psycopg documentation... obvious huh?



                to send notification using



                cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


                one have to set connection to autocommit like that



                import psycopg2
                import psycopg2.extensions
                conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)


                or simply if you don't want autocommit then after doing



                cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


                you have to commit it



                conn.commit()


                aaand now Node is receiving notifications from postgresql via python






                share|improve this answer












                Oh my... i just found solution... even two ways to solve this.



                The clue was in psycopg documentation... obvious huh?



                to send notification using



                cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


                one have to set connection to autocommit like that



                import psycopg2
                import psycopg2.extensions
                conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT)


                or simply if you don't want autocommit then after doing



                cur.execute("SELECT pg_notify('%s', '%s');" % ('testnotify', 'blabla'))


                you have to commit it



                conn.commit()


                aaand now Node is receiving notifications from postgresql via python







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 8 at 11:28









                Paabo

                62




                62






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53205675%2fpython-psycopg2-executing-select-pg-notify-doesnt-work%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

                    Schultheiß

                    Liste der Kulturdenkmale in Wilsdruff

                    Android Play Services Check