GithubHelp home page GithubHelp logo

Comments (3)

larskanis avatar larskanis commented on August 13, 2024

The sequence \set ON_ERROR_STOP true is a psql internal command. It is interpret in psql on the client side. It has nothing to do with the PostgreSQL server nor with ruby-pg. They don't recognize psql's backslash commands.

Does the file seed.sql run successfully when you remove the \set by gsub? It should work when the file uses plain SQL commands. However if it contains COPY sequences like pg_dump generates per default, it needs special handling through PG::Connection#put_copy_data and a parser which separates between #exec and #put_copy_data. The COPY handling is another extension of psql. If pg_dump is executed with --insert then the SQL text should work with PG::Connection#exec without further efforts.

from ruby-pg.

esparta avatar esparta commented on August 13, 2024

I did elaborate this answer directly on Mastodon, sorry about the duplication:

PostgreSQL, the server, only recognize DDL, DQL, DML, DCL and TCL Commands. When you send an string starting with \ to the server using pg then the gem will tell you what the PostgreSQL say: ERROR.

In other words, sentences starting with \ are only recognized by psql, the tool, as commands. psql would take that string and intercept them for their own purposes, but doesn't send it to PostgreSQL.

Maybe the documentation should be more clear about it:
https://www.postgresql.org/docs/current/app-psql.html

The same would happen with other \ commands such as \dt which in psql list the tables on the current database and schema:

DB = PG.connect(dbname: "mydbname")
r = DB.exec('\dt')
Traceback (most recent call last):
        2: from (irb):9
        1: from (irb):9:in `exec'
PG::SyntaxError (ERROR:  syntax error at or near "\")

If you want to accomplish the same as \dt using raw sql sent to PostgreSQL, then you need to use the PostgreSQL catalog and do what psql do internally:

DB = PG.connect(dbname: "mydbname")
r = DB.exec("SELECT tablename FROM pg_catalog.pg_tables where schemaname='public';")
r.to_a
# => => [{"tablename"=>"active_storage_blobs"}, {"tablename"=>"active_admin_comments"} .. ]

Hope this helps.

from ruby-pg.

schneems avatar schneems commented on August 13, 2024

I was able to get it to work by removing the line and moving the error checking flag to the psql command directly:

psql -v ON_ERROR_STOP=1 --dbname=mydbname --file=config/schema.sql

Leaving this note to let others who might run into the same issue.

Does the file seed.sql run successfully when you remove the \set by gsub? It should work when the file uses plain SQL commands.

Yep, it works fine then. I guess it doesn't make sense for the pg gem to have a "run this command as if it was psql" as that might be hard to maintain. I wonder if there's a small set of syntax it might make sense to detect and annotate the error.

Like: "Note: Not all syntax in psql can be executed by the pg gem, for more information: "

However, I'm not sure how often people might run into something like this, maybe shelve the idea until/unless it comes up again?

from ruby-pg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.