Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Insert data into postgres using bash

I’m trying to insert values into postgres using bash. During my searching, I did find this:

Inserting data into postgres db using shell script

The script I wrote is not working, and giving the following errors:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ERROR: column "task does not exist
LINE 1: INSERT INTO my_table (task,date,quad,branch) VALUES (task,2024-0...
                                                             ^
HINT:  There is a column named "task" in table "my_table", but it cannot be referenced from this part of the query.

My script is here:

#!/bin/bash
server=my_server
database=postgres
user=my_user

taskInput="task"
dateInput="2024-01-03"
quadinput="input_quad"
branchInput="shipping"
psql -U $user -w -h $server -p 5432 -d $database -c  "INSERT INTO my_table (task,date,quad,branch) VALUES ($taskInput,$dateInput,$quadInput,$branchInput)"

I’m not sure what’s going on, and hoping you smart people can enlighten me.

>Solution :

Because you’re building the SQL string yourself (and not using a library), you must provide quotes around strings:

psql -U $user -w -h $server -p 5432 -d $database -c "INSERT INTO my_table (task,date,quad,branch) VALUES ('$taskInput','$dateInput','$quadInput','$branchInput')"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading