Comments (3)
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.
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.
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)
- Seg fault after upgrading to pg v1.5.2 HOT 3
- Saw Rails DB connection pool spike after upgrading from 1.4.6 to 1.5.2 HOT 5
- failed to install pg 1.4.6 on Centos7 with ruby 3.2.0, whereas with ruby 3.1.0 it installs HOT 3
- How to build an old pg on ruby 2.3.1 HOT 1
- Segmentation fault with ruby 2.7.8 and pg 1.5.3 HOT 10
- Segmentation fault HOT 28
- Sonoma: getaddrinfo segmentation fault HOT 2
- Segmentation fault in ruby:3.2.2-alpine3.18 docker image HOT 2
- Wrong binary conversion on x86 (32bit intel) arch causing test failures HOT 4
- Bad IAM Token against Amazon Aurora Postgres: Out of memory error HOT 2
- conn.exec writing "INFO" statements to stderr instead of stdout HOT 2
- Segfault on ruby 3.2.2, pg 1.5.4 HOT 2
- Error ruby(39749,0x1e1f05300) malloc: double free for ptr 0x13c6bfa00 HOT 1
- Segmentation fault when connecting to RDS HOT 11
- Broken links in documentation HOT 3
- connection.reset don't check again DNS HOT 6
- Usage with ApacheAGE for Postgresql HOT 2
- Add BigDecimal to gemspec for Ruby 3.3/4 HOT 2
- Binary Windows gem no longer available for Ruby v3.3 since v1.5.6 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ruby-pg.