-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-database.sh
548 lines (461 loc) · 17.6 KB
/
create-database.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#!/bin/bash
# This shell script creates a database and sets it up for further convenient
# analysis in R.
# Set up directories and variable values
# Set up directory locations
psql=/cygdrive/c/Program\ Files/PostgreSQL/9.5/bin/psql.exe
createuser=/cygdrive/c/Program\ Files/PostgreSQL/9.5/bin/createuser.exe
zipDir="/cygdrive/c/Program Files/7-zip/"
copycatsDir=$(pwd)
copycatsDirWin="D:/Cloud\ Storages/GitHub/copycats-analysis/"
# Set up usernames and passwords (not recommended to change)
username=copycat
dbname=copycats
password=default-password-for-copycats
database=copycats
# default username and databse (username was provided during installation
# process; database is the one that already exist)
postgresUser=postgres
postgresDb=postgres
# Change directory to PostgreSQL/bin directory
# cd "$psqlDir"
# Create a user
echo ---------------------------------------------------------------------------
# Check if user exists
# The following query returns nothing (is empty) if the user does not exist.
sqlOutput=`"$psql" -U $postgresUser -tAc \
"SELECT 1 FROM pg_user WHERE usename = '$username';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# User exists. No need to create
echo User $username already exists. No need to create a new one.
else
# User does no exist
echo User $username does not exist. Creating a new one.
"$createuser" --createdb --username $postgresUser --no-createrole \
--unencrypted $username
# Check for error codes after creating a user
if [ $? -ne 0 ] ; then
echo Creating user exited with errors.
exit 2
fi
echo User $username created succesfuly.
echo Changing $username\'s password.
"$psql" -c "ALTER ROLE $username WITH PASSWORD '$password';" $postgresDb \
$postgresUser
# Check for error codes after changing the password of the user
if [ $? -ne 0 ] ; then
echo Changing password exited with errors.
exit 2
fi
echo Password for user $username changed succesfully.
# Append password to password management file (to avoid typing password for
# this user again
echo "localhost:5432:*:$username:$password">>$APPDATA/postgresql/pgpass.conf
if [ $? -ne 0 ] ; then
echo Storing password in pgpass.conf exited with errors.
exit 2
fi
echo Password stored in pgpass.conf succesfully.
fi
# Create copycats database
echo ---------------------------------------------------------------------------
# Check if database exists
# The following query returns nothing (is empty) if the databse exist.
sqlOutput=`"$psql" -U $postgresUser -tAc \
"SELECT 1 FROM pg_database WHERE datname = '$database';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Databse exists. No need to create
echo Databse $database already exists. No need to create a new one.
else
# Databse does no exist
echo Databse $database does not exist. Creating a new one.
"$psql" -c "CREATE DATABASE $database" $postgresDb $username
if [ $? -ne 0 ] ; then
echo Creating a databse $database exited with errors.
exit 2
fi
echo Database $databse created succesfully.
fi
# Create schemas
echo ---------------------------------------------------------------------------
# Check if schemas exist
# tr schema
# The following query returns nothing (is empty) if the databse exist.
schema=tr
sqlOutput=`"$psql" -U $username -d $database -tAc \
"SELECT 1 FROM information_schema.schemata WHERE schema_name = '$schema';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Schema exists. No need to create
echo Schema $schema already exists. No need to create a new one.
else
#Schema does no exist
echo Schema $schema does not exist. Creating a new one.
"$psql" -U $username -d $database -c \
"CREATE SCHEMA $schema"
if [ $? -ne 0 ] ; then
echo Creating a schema $schema exited with errors.
exit 2
fi
echo Schema $schema created succesfully.
fi
# crsp schema
# The following query returns nothing (is empty) if the databse exist.
schema=crsp
sqlOutput=`"$psql" -U $username -d $database -tAc \
"SELECT 1 FROM information_schema.schemata WHERE schema_name = '$schema';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Schema exists. No need to create
echo Schema $schema already exists. No need to create a new one.
else
# Schema does no exist
echo Schema $schema does not exist. Creating a new one.
"$psql" -U $username -d $database -c \
"CREATE SCHEMA $schema"
if [ $? -ne 0 ] ; then
echo Creating a schema $schema exited with errors.
exit 2
fi
echo Schema $schema created succesfully.
fi
# mflinks schema
# The following query returns nothing (is empty) if the databse exist.
schema=mflinks
sqlOutput=`"$psql" -U $username -d $database -tAc \
"SELECT 1 FROM information_schema.schemata WHERE schema_name = '$schema';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Schema exists. No need to create
echo Schema $schema already exists. No need to create a new one.
else
# Schema does no exist
echo Schema $schema does not exist. Creating a new one.
"$psql" -U $username -d $database -c \
"CREATE SCHEMA $schema"
if [ $? -ne 0 ] ; then
echo Creating a schema $schema exited with errors.
exit 2
fi
echo Schema $schema created succesfully.
fi
# stocks schema
# The following query returns nothing (is empty) if the databse exist.
schema=stocks
sqlOutput=`"$psql" -U $username -d $database -tAc \
"SELECT 1 FROM information_schema.schemata WHERE schema_name = '$schema';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Schema exists. No need to create
echo Schema $schema already exists. No need to create a new one.
else
# Schema does no exist
echo Schema $schema does not exist. Creating a new one.
"$psql" -U $username -d $database -c \
"CREATE SCHEMA $schema"
if [ $? -ne 0 ] ; then
echo Creating a schema $schema exited with errors.
exit 2
fi
echo Schema $schema created succesfully.
fi
# Create tables
echo ---------------------------------------------------------------------------
# Create tr tables
# Check if tables already exist. (Check only one table and assume that if one
# table exist, then the other ones too, by the nature of the script: if it has
# been run once, then all tables should already exist)
schema=tr
table=holdings_change # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT 1 FROM information_schema.tables \
WHERE table_schema = '$schema' \
AND tables.table_name = '$table';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Table exists. No need to create
echo Table $schema.$table already exists. No need to create new $schema \
tables.
else
# Table does no exist
echo Table $schema.$table does not exist. Creating new tables.
"$psql" -U $username -d $database -f "./sql/create-tables-$schema.sql"
if [ $? -ne 0 ] ; then
echo Creating tables in $schema exited with errors.
exit 2
fi
echo Tables in $schema created succesfully.
fi
# Create crsp tables
# Check if tables already exist. (Check only one table and assume that if one
# table exist, then the other ones too, by the nature of the script: if it has
# been run once, then all tables should already exist)
schema=crsp
table=portfolio_holdings # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT 1 FROM information_schema.tables \
WHERE table_schema = '$schema' \
AND tables.table_name = '$table';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Table exists. No need to create
echo Table $schema.$table already exists. No need to create new $schema \
tables.
else
# Table does no exist
echo Table $schema.$table does not exist. Creating new tables.
"$psql" -U $username -d $database -f "./sql/create-tables-$schema.sql"
if [ $? -ne 0 ] ; then
echo Creating tables in $schema exited with errors.
exit 2
fi
echo Tables in $schema created succesfully.
fi
# Create mflinks tables
# Check if tables already exist. (Check only one table and assume that if one
# table exist, then the other ones too, by the nature of the script: if it has
# been run once, then all tables should already exist)
schema=mflinks
table=link2 # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT 1 FROM information_schema.tables \
WHERE table_schema = '$schema' \
AND tables.table_name = '$table';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Table exists. No need to create
echo Table $schema.$table already exists. No need to create new $schema \
tables.
else
# Table does no exist
echo Table $schema.$table does not exist. Creating new tables.
"$psql" -U $username -d $database -f "./sql/create-tables-$schema.sql"
if [ $? -ne 0 ] ; then
echo Creating tables in $schema exited with errors.
exit 2
fi
echo Tables in $schema created succesfully.
fi
# Create mflinks tables
# Check if tables already exist. (Check only one table and assume that if one
# table exist, then the other ones too, by the nature of the script: if it has
# been run once, then all tables should already exist)
schema=stocks
table=daily # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT 1 FROM information_schema.tables \
WHERE table_schema = '$schema' \
AND tables.table_name = '$table';"`
if [[ ${#sqlOutput} -gt 0 || $sqlOutput == "1" ]]
then
# Table exists. No need to create
echo Table $schema.$table already exists. No need to create new $schema \
tables.
else
# Table does no exist
echo Table $schema.$table does not exist. Creating new tables.
"$psql" -U $username -d $database -f "./sql/create-tables-$schema.sql"
if [ $? -ne 0 ] ; then
echo Creating tables in $schema exited with errors.
exit 2
fi
echo Tables in $schema created succesfully.
fi
# Populate tables from csv files
echo ---------------------------------------------------------------------------
export PATH=$PATH:"$zipDir"
# tr
# Check if tables are already populated. (Again, only check one table)
schema=tr # last table in the script
table=holdings_change # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT * FROM $schema.$table LIMIT 1;"`
if [ ${#sqlOutput} -ne 0 ]
then
# Table is populated. Not need to populate again
echo $schema.$table contains at least one entry. No need to import data into\
$schema tables.
else
# Table is not populated. Need to populate it
echo $schema.$table contains no entries. Populating $schema tables.
# Some of the tables need to be cleaned before they can be imported into
# Postgres
echo Create temporary files.
zcat ./data/raw/wrds-tr-s12-type1-197901-201509.csv.gz | tr -d \
'\200-\377' | gzip > \
./data/clean/wrds-tr-s12-type1-197901-201509.csv.gz
echo Temporary type 1 data files created succesfully.
"$psql" -U $username -d $database -f \
"./sql/copy-csv-$schema.sql"
if [ $? -ne 0 ] ; then
echo Populating $schema tables exited with errrors.
exit 2
fi
echo Tables $schema populated succesfully.
echo Remove temporary files
rm ./data/clean/*
if [ $? -ne 0 ] ; then
echo Removing temporary files exited with errors.
exit 2
fi
echo Temporary tr files removed succesfully.
fi
# crsp
# Check if tables are already populated. (Again, only check one table)
schema=crsp # last table in the script
table=rear_load # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT * FROM $schema.$table LIMIT 1;"`
if [ ${#sqlOutput} -ne 0 ]
then
# Table is populated. Not need to populate again
echo $schema.$table contains at least one entry. No need to import data into\
$schema tables.
else
# Table is not populated. Need to populate it
echo $schema.$table contains no entries. Populating $schema tables.
# Some of the tables need to be cleaned before they can be imported into
# Postgres
echo Create temporary files.
# Daily returns
# Replace incorrect missing value characters with nothing
zcat ./data/raw/wrds-crsp-mfdb-daily-returns-19980901-20151231.csv.gz | \
awk -F, -f ./awk/crsp-daily-return.awk | \
gzip > ./data/clean/wrds-crsp-mfdb-daily-returns-19980901-20151231.csv.gz
echo Temporary clean daily returns file created succesfully.
# Fund portfolio map
# Clean file from non-ASCII characters
zcat ./data/raw/wrds-crsp-mfdb-fund-portfolio-map.csv.gz | tr -d \
'\200-\377' | gzip > \
./data/clean/wrds-crsp-mfdb-fund-portfolio-map.csv.gz
echo Temporary clean fund portfolio map file created succesfully.
# Fund summary
# Change three quotes to one quote and clean from non-ASCII characters
TEMP=$(mktemp)
zcat ./data/raw/wrds-crsp-mfdb-fund-summary-196112-2015-12.csv.gz | \
sed -e 's/\"\"\"/\"/g' | tr -d '\200-\377' | gzip > $TEMP
echo Temporary file with removed excess quotation created.
# Replace incorrect missing values characters with nothing
zcat $TEMP | \
awk -vFPAT='[^,]*|"[^"]*"' -f./awk/crsp-fund-summary.awk | \
gzip > ./data/clean/wrds-crsp-mfdb-fund-summary-196112-2015-12.csv.gz
echo Temporary fund summary files created succesfully.
rm $TEMP
echo Temporary file with removed excess quotation removed.
echo Temporary clean fund summary file created succesfully.
# Monthly returns
# Replace incorrect missing values characters with nothin
zcat ./data/raw/wrds-crsp-mfdb-monthly-returns-196112-2015-12.csv.gz | \
awk -F, -f ./awk/crsp-monthly-return.awk | \
gzip > ./data/clean/wrds-crsp-mfdb-monthly-returns-196112-2015-12.csv.gz
echo Temporary daily returns file created succesfully.
# Portfolio holdings
# Clean from non-ASCII characters
zcat ./data/raw/wrds-crsp-mfdb-portfolio-holdings-200101-2015-12.csv.gz | \
tr -d '\200-\377' | gzip > \
./data/clean/wrds-crsp-mfdb-portfolio-holdings-200101-2015-12.csv.gz
echo Temporary portfolio holdings file created succesfuly.
echo Import data
"$psql" -U $username -d $database -f \
"./sql/copy-csv-$schema.sql"
if [ $? -ne 0 ] ; then
echo Populating $schema tables exited with errrors.
exit 2
fi
echo Tables $schema populated succesfully.
echo Remove temporary files
rm ./data/clean/*
if [ $? -ne 0 ] ; then
echo Removing temporary files exited with errors.
exit 2
fi
echo Temporary crsp files removed succesfully.
fi
# mflinks
# Check if tables are already populated. (Again, only check one table)
schema=mflinks # last table in the script
table=link2 # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT * FROM $schema.$table LIMIT 1;"`
if [ ${#sqlOutput} -ne 0 ]
then
# Table is populated. Not need to populate again
echo $schema.$table contains at least one entry. No need to import data into\
$schema tables.
else
# Table is not populated. Need to populate it
echo $schema.$table contains no entries. Populating $schema tables.
# Clean file from non-ASCII characters
cat ./data/mflinks/link1.csv | tr -d '\200-\377' | \
sed 's/,"/,QUOTEPLACEHOLDER/g' | \
sed 's/",/QUOTEPLACEHOLDER,/g' | \
sed 's/"$/QUOTEPLACEHOLDER/g' | \
sed 's/"//g' | \
sed 's/QUOTEPLACEHOLDER/"/g' > \
./data/clean/link1.csv
echo Temporary clean link1 file created succesfully.
cat ./data/mflinks/link2.csv | tr -d '\200-\377' > \
./data/clean/link2.csv
echo Temporary clean link2 file created succesfully.
"$psql" -U $username -d $database -f \
"./sql/copy-csv-$schema.sql"
if [ $? -ne 0 ] ; then
echo Populating $schema tables exited with errrors.
exit 2
fi
echo Tables $schema populated succesfully.
echo Remove temporary files
rm ./data/clean/*
if [ $? -ne 0 ] ; then
echo Removing temporary files exitted with errors.
exit 2;
fi
echo Temporary files removed succesfully.
fi
# stocks
# Check if tables are already populated. (Again, only check one table)
schema=stocks # last table in the script
table=daily # last table in the script
sqlOutput=`"$psql" -U copycat -d copycats -tAc \
"SELECT * FROM $schema.$table LIMIT 1;"`
if [ ${#sqlOutput} -ne 0 ]
then
# Table is populated. Not need to populate again
echo $schema.$table contains at least one entry. No need to import data into\
$schema tables.
else
# Table is not populated. Need to populate it
echo $schema.$table contains no entries. Populating $schema tables.
# Replace incorrect missing values characters with nothing
echo Create temporary daily returns file.
zcat ./data/raw/wrds-crsp-stocks-daily-19900101-20153112.csv.gz | \
awk -F, -f ./awk/crsp-stocks-daily.awk | \
gzip > ./data/clean/wrds-crsp-stocks-daily-19900101-20153112.csv.gz
echo Temporary daily returns file created succesfully.
"$psql" -U $username -d $database -f \
"./sql/copy-csv-$schema.sql"
if [ $? -ne 0 ] ; then
echo Populating $schema tables exited with errrors.
exit 2
fi
echo Tables $schema populated succesfully.
echo Remove temporary files
rm ./data/clean/*
if [ $? -ne 0 ] ; then
echo Removing temporary files exitted with errors.
exit 2;
fi
echo Temporary files removed succesfully.
fi
# Index tables
echo ---------------------------------------------------------------------------
# Create table indeces
echo Create indeces
"$psql" -U $username -d $database -f './sql/index-tables.sql'
# Import cash and other data
bash import-cash-other-returns.sh
# Create clean dataset
bash create-clean-dataset.sh
exit 0