Search code examples
linuxpostgresqlcsv-import

Script to automat import of CSV into PostgreSQL


I need to import a huge number of CSV files into Postgres (Linux). Does anybody know how I can write a script to automate this process?

I am a complete novice. Please help!


Solution

  • You can make a shell script like loop.sh

    #!/bin/sh
    
    files=`ls *.sql`
    
    for file in $files
    do
      echo $file
      psql database-name < $file
    done
    

    Make the script executable by

    chmod 0755 loop.sh
    

    and run it

    ./loop.sh