I am new to database management and we are using psql. All I need to do is to migrate csv (around 200 tables) to our database. Manually creating tables for every csv file is bit tiresome so please help me out, Is there any way to generate table from csv file?
-
3You can use COPY to do the job, see thisVivek S.– Vivek S.2015年09月01日 09:09:22 +00:00Commented Sep 1, 2015 at 9:09
-
2This is what you need to doVivek S.– Vivek S.2015年09月01日 09:12:33 +00:00Commented Sep 1, 2015 at 9:12
-
thanks, does this copy command will populate the table from csv file???saniojoseph– saniojoseph2015年09月01日 11:59:47 +00:00Commented Sep 1, 2015 at 11:59
-
You're going to have to write a script that reads in the csv files and writes out create table statements.user464502– user4645022015年09月01日 13:27:40 +00:00Commented Sep 1, 2015 at 13:27
-
i would really appreciate if any one could help me out to find a script that will automatically populate the table from csv. its just am new to back end, pleasesaniojoseph– saniojoseph2015年09月03日 04:58:41 +00:00Commented Sep 3, 2015 at 4:58
1 Answer 1
Answered at DBA Stackexchange by the OP. I'm copying the answer here because this was the first link returned by my search engine.
OP made a script like:
DATADIR='data' # this directory name
PREFIX='jobd'
DBNAME='divacsv'
function createSchema {
COLUMNS=`head -n 1 1ドル |
awk -F, '{for(i=1; i<=NF; i++){out=out $i" text, ";} print out;}' |
sed 's/ text, $/MYEXTRA text/' |
sed 's/"//g'`
CMD_CREATE="psql $DBNAME -c \"CREATE TABLE 2ドル ($COLUMNS);\""
echo $CMD_CREATE
sh -c "$CMD_CREATE"
CMD_COPY="psql divacsv -c \"COPY 2ドル FROM '"`pwd`"/1ドル' DELIMITER ',' CSV;\""
echo $CMD_COPY
sh -c "$CMD_COPY"
}
for file in $DATADIR/*.csv; do
table=$PREFIX"_"`echo $file | sed 's/.*\///' | sed 's/.csv//' `
createSchema "$file" $table
done
Comments advise that HEADER might be needed to avoid loading first line with header texts, which is true.
I've tested this code but couldn't make it work under CentOS.
Comments
Explore related questions
See similar questions with these tags.