Can Powershell Pipe Input Like Bash’s Here Document?

0
20
Asked By WanderingTurtle88 On

I'm working on a way to insert sanitized data into a PostgreSQL database, and I stumbled upon an interesting challenge. In Bash, you can use the `<<<` operator to pipe commands directly into Postgres' command line tool `psql` along with session variables defined by the `-v` flag. However, I'm wondering if Powershell has a similar capability because I want to send commands from right to left like Bash does. The tricky part is that when using a regular pipeline, it seems I get a syntax error since the session variables aren't defined until after the input is piped. I'm looking for a solution that doesn't involve ODBC or using PostgreSQL's `set` command to define parameters within the insert statement. Anyone have any ideas?

3 Answers

Answered By CosmicHedgehog99 On

Have you tried using the Bash-like `<<<` operator? It might be managed by `psql` itself rather than Powershell. If you give that a shot, it might just work for your use case!

Answered By CuriousCat42 On

The way Powershell handles scopes is different from Bash, so you actually might not need that operator. Just remember, the pipeline input is sent to `psql` when it expects it from stdin. And make sure to include `-v` for each variable you’re assigning; otherwise, you’ll run into issues.

Answered By TechSavvySquirrel On

What about using the `-c` argument to provide the SQL statement instead of using stdin? Like this: `psql $databaseSSH -v a="A" b="B" c="C" -c "INSERT INTO table (a, b, c) VALUES (:'a', :'b', :'c');"` Also, I’ve found that using an SQL file can help organize things better. Just specify `psql -f -v ...`, and it skips the standard input altogether.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.