Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1maekG-0001jn-Ky for psycopg@arkaria.postgresql.org; Wed, 13 Oct 2021 13:56:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1maekE-0003OI-IB for psycopg@arkaria.postgresql.org; Wed, 13 Oct 2021 13:56:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1maekE-0003O9-AN for psycopg@lists.postgresql.org; Wed, 13 Oct 2021 13:56:34 +0000 Received: from mail-ua1-x936.google.com ([2607:f8b0:4864:20::936]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1maek7-0007vh-Iu for psycopg@postgresql.org; Wed, 13 Oct 2021 13:56:33 +0000 Received: by mail-ua1-x936.google.com with SMTP id h4so4709484uaw.1 for ; Wed, 13 Oct 2021 06:56:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=Xqi1asHrorIGnAjvXS8vengVT6unKnKxfIHPBsJtSG0=; b=A4g0HDCk+/ul5AEYGSlDvnsjf/mBQgIdzTV9vGbqEYRwkG3E6cifVWq/SAkk8zBJqT qtHIOpn41tq6W+QYGx+533rcaUWCXbmmUoWraC+RTbzjD1oscnRCqFQMl3/2dK8Cyel/ iOc1I5M34iwylMFK2afXEIv2ltAsb75nIUkKhtWplfp8IekON2ogCrJsEaMFvBfS4KVN C9H/mbZ7VH6gdUBW3xEz04Ou0+EwP3Psm5+MVjmir1PrwQn7MvkfM2HwLQWxbwNP3aIc utqScQHTO/X6MNpTfv1jVscQr1R4Gni8FxCgnAYoMdtrW6prQ5hp3TDZKmpzoH1I3AXy zqfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=Xqi1asHrorIGnAjvXS8vengVT6unKnKxfIHPBsJtSG0=; b=LHgGhFWB5qD3N+4xf7DveK53G3fAlUxQBYlUF9Hu0DtDomEztIwvqTC4uIa+qRtG9B C8H/0GUfw47pdfxq9OqywvJwmVUA+uvTy+8PnCCd+KEd1rEYYJ3CwwskSTJZPxVBdWZ9 UU9KedFn52HFvy/ucwIkIjwz/9MqqxYi9pVjY7/GJ3fK7Ltvzr08K6ECqf4c8hsltCeH z/YKkW/m/0leP4EEu5xyeQxXxDvXfu9lFSbEClV8p4h68+3deefm8EfA9BaVcMP8/p1V E/m25S5Tur/endGJG7qANR6QUCJP8dMjCbxtrkjHZsz9dfid37Y0Nq2arpwJcunEMzpf eBVQ== X-Gm-Message-State: AOAM530d6cJiCgIjVTqBeJgEMd2dJ6HFZSX9HPS8+FQO5+2qr6wm3SiE B2CaqRN+IjWunn8Fan5Eyw5LBmQcQ40TEC+9+QpQpfSmV6w= X-Google-Smtp-Source: ABdhPJwpcjYVDKjvyPnFkqtQ37TfWRPA2j7YhtoJFp7YsiWRYBk6Ma75kr/tb5ccByjhgZfvYcEnTLep/7uubL1JBTc= X-Received: by 2002:ab0:5b90:: with SMTP id y16mr30587328uae.43.1634133385798; Wed, 13 Oct 2021 06:56:25 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Daniele Varrazzo Date: Wed, 13 Oct 2021 15:56:14 +0200 Message-ID: Subject: Re: Using standard SQL placeholders in PG To: "Lembark, Steven" Cc: "psycopg@postgresql.org" , "Loga, Dharani" Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, 13 Oct 2021 at 15:44, Lembark, Steven wrote: >... The doller-notation allows re-use of positional parameters and avoids = SQL injection entirely by placing the input is appropriately-typed buffers = w/in the SQL statement handler. It also avoids issues with double-interpola= ting quotes as the placeholders are not literals and do not require SQL-quo= ting to be interpolated properly. You still require to convert items to string in the right format understood by Postgres, which might be different from the Python string form. > Q: Is there any way to prepare and execute standard PG SQL statements wit= h dollar placeholders in PsychoPG2? No. psycopg2 is entirely built around "convert to postgres format and add quotes". We have just released Psycopg 3 exactly to solve this kind of problem. It also has no direct access to PQexecParams, but it only uses PQexec so there's no easy way to plug in the $1 params. Note that still Psycopg 3 doesn't allow you to use $1 placeholders, but they are the ones it uses internally. There is an internal object, PostgresQuery, that takes a %s-style query and, among other things, converts it in $n format. It wouldn't be a difficult thing to write a cursor to do less work and just convert the arguments. In Psycopg 3 you can also go a level lower and call directly PQexecParams passing the query and arguments you want, in postgres format, but then you have to do the conversion from Python objects to Postgres format yourself. I would rather put together a cursor to do so, as above. So, things can be done, but in Psycopg 3. -- Daniele