How to write bash script to insert the records from file my_records.txt into PostgreSQL DB table_ - DEV Community
How to write bash script to insert the records from file my_records.txt into PostgreSQL DB table_ - DEV Community
Dmitry Romanoff
Posted on 11 de abr. de 2023 • Edited on 14 de mai.
6 1 1
First step, create the table of the corresponding structure in the PostgreSQL DB.
Here's a bash script to insert the records from a file called "my_records.txt" into a
PostgreSQL database table:
#!/bin/bash
https://dev.to/dm8ry/how-to-write-bash-script-to-insert-the-records-from-file-myrecordstxt-into-postgresql-db-table-45cd 1/6
04/12/2024, 00:18 How to write bash script to insert the records from file my_records.txt into PostgreSQL DB table? - DEV Community
#!/bin/bash
dmi@dmi-laptop:~$ ./populate_file_recs_into_pg_table.sh
INSERT 0 1
INSERT 0 1
INSERT 0 1
INSERT 0 1
https://dev.to/dm8ry/how-to-write-bash-script-to-insert-the-records-from-file-myrecordstxt-into-postgresql-db-table-45cd 2/6
04/12/2024, 00:18 How to write bash script to insert the records from file my_records.txt into PostgreSQL DB table? - DEV Community
INSERT 0 1
dmi@dmi-laptop:~$
Here's an updated script that adds a check to ensure that the number of inserted
records matches the number of records in the table:
#!/bin/bash
export DB_HOST="localhost"
export DB_PORT="5442"
export DB_NAME="postgres"
export DB_USER="myuser"
export PGPASSWORD='mypwd'
export DB_NAME="postgres"
export TABLE_NAME="your_table_name"
export RECORDS_FILE="my_records.txt"
# Compare the counts to ensure that the correct number of records were inserted
if [ "$count_after" -eq $(( "$count_before" + "$count_num_of_recs_in_the_file" )) ];
echo "All records were inserted successfully."
else
echo "Error: Incorrect number of records inserted."
fi
This script adds two SELECT COUNT(*) queries to count the number of records in the
table before and after the insert. It then compares the counts to ensure that the
correct number of records were inserted. The expected number of records in the
table is calculated by adding the number of records in the file to the count before the
insert.
In this code, I've used a copy approach to populate the table with the records.
https://dev.to/dm8ry/how-to-write-bash-script-to-insert-the-records-from-file-myrecordstxt-into-postgresql-db-table-45cd 3/6
04/12/2024, 00:18 How to write bash script to insert the records from file my_records.txt into PostgreSQL DB table? - DEV Community
AWS PROMOTED
Register now
https://dev.to/dm8ry/how-to-write-bash-script-to-insert-the-records-from-file-myrecordstxt-into-postgresql-db-table-45cd 4/6
04/12/2024, 00:18 How to write bash script to insert the records from file my_records.txt into PostgreSQL DB table? - DEV Community
Speechmatics PROMOTED
Dmitry Romanoff
JOINED
18 de set. de 2022
https://dev.to/dm8ry/how-to-write-bash-script-to-insert-the-records-from-file-myrecordstxt-into-postgresql-db-table-45cd 5/6
04/12/2024, 00:18 How to write bash script to insert the records from file my_records.txt into PostgreSQL DB table? - DEV Community
Neon PROMOTED
See Article
https://dev.to/dm8ry/how-to-write-bash-script-to-insert-the-records-from-file-myrecordstxt-into-postgresql-db-table-45cd 6/6