2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

postgresqlのCSV流し込みをWindowsのbatファイルから実行するサンプル

Last updated at Posted at 2018-02-18

CSVファイルをPostgresqlに流し込みたいときに、
流し込み対象ファイルの置いてあるフォルダにこのbatファイルを格納して、
batファイルをダブルクリックすれば流し込みされる。

もしも複数件見つかってしまったら(そんなことはないようにするが)、
最後に見つかったものが流し込まれる。

流し込み対象ファイル(180218_import.csv)

h1,h2
1,2
3,4
5,6
7,8

流し込み用batファイル

@echo off

rem setup path
set TIME_STAMP=%DATE:~-10,4%%DATE:~-5,2%%DATE:~-2%%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
set CURRENT_PATH=%~dp0
FOR /F %%i in ('dir /b *import.csv') do set IMPORT_FILE=%CURRENT_PATH%%%i

rem postgres info
set HOST=localhost
set PORT=5432
set USERID=postgres
set DBNAME=postgres

rem exec query
psql -h %HOST% -p %PORT% -U %USERID% -d %DBNAME% -c "CREATE TABLE import_%TIME_STAMP% (h1 text, h2 text)"
psql -h %HOST% -p %PORT% -U %USERID% -d %DBNAME% -c "COPY import_%TIME_STAMP% FROM '%IMPORT_FILE%' with csv header"

pause
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?