Making a batch using windows, psql. I have this copy statement
@echo off
SETLOCAL
REM Set variables
SET "DB_HOST=localhost"
SET "DB_PORT=5432"
SET "DB_NAME=postgres"
SET "DB_USER=postgres"
SET "DB_PASSWORD=postgres"
SET "CSV_DIR=C:UsersyujuSQL_Data"
SET "TABLE_NAME=dibbs_db"
REM Find the latest CSV file in the directory
FOR /F "delims=" %%I IN ('dir "%CSV_DIR%*.csv" /B /O:D /T:C') DO SET "LATEST_CSV=%%I"
REM Full path to the latest CSV file
SET "LATEST_CSV_PATH=%CSV_DIR%%LATEST_CSV%"
REM Print the file being imported (for logging purposes)
echo Importing %LATEST_CSV%
REM Run the psql command to import the CSV
psql -h %DB_HOST% -p %DB_PORT% -d %DB_NAME% -U %DB_USER% -c "copy "Dibbs".dibbs_db(customer_name, Quote_number, Polytronix_Part_number, Order_Status, Bid_Total, Unit_Price, Customer_Part_number, Most_Recent_Unit_Cost, Most_Recent_Quantity, Total_Potential, Competitors, Solicitation_number, NSN_part_number, Date_Accessed, Closing_Date, Quantity_Requested, Person_name, PR_number, Request_For_Quotation_Forms, Tech_Docs, Tech_Docs_Links, Qualifications, Nomenclature, Reason, Spec_Summary, FAI) FROM 'C:UsersyujuSQL_Data' WITH DELIMITER ',' CSV HEADER"
PAUSE
REM Check the exit status of the psql command
IF %ERRORLEVEL% NEQ 0 (
echo Failed to import %LATEST_CSV_PATH%
EXIT /B %ERRORLEVEL%
) ELSE (
echo Successfully imported %LATEST_CSV_PATH%
)
ENDLOCAL
EXIT /B 0
I get the error message
“Importing solicitation0 – Copy.csv
Password for user postgres:
C:/Users/yuju/SQL_Data: Permission denied”
It echos the csv correctly so I’m assuming the copy statement is not formatted correctly somewhere. Tried taking just the copy statement and it works if I copy first half(-d -u -h-p), password, and second half (copy statement) but with the batch it gives me errors.
End goal: I want to make a batch file to find the most recent CSV and import that into my postgres table.