I have a table I routinely want to drop, then re-import and add a primary key constraint for.
The thing is I don't want to step thru the wizard each time.
How can I script this as a command so I don't need to interact with a GUI/wizard?
If it is of significance I am using PostgreSQL on a Mac
Asked
Active
Viewed 2,791 times
0

JGFMK
- 8,425
- 4
- 58
- 92
-
i would write a plpgsql function for it. If the csv changes name, use it as a parameter, in the function do your drop table and so on . to import a file from csv use the copy command ( maybe read access for the csv file needs adjustment ) and finaly write a task or cronjob equivalent on mac witch calls the function eg select public.myimport('full/path/myfile.csv') – FatFreddy May 11 '22 at 10:24
-
Ok. Thanks for that tip. I'll have a dig around in the PostreSQL docs for this type of thing. – JGFMK May 11 '22 at 12:20
-
https://www.postgresqltutorial.com/postgresql-tutorial/import-csv-file-into-posgresql-table/ – JGFMK May 11 '22 at 12:20
-
I actually ended up having to use a combo of script to drop/create files as the data types of columns were too varied to do generically. The only other gotcha was I had to use psql command to use `\copy table from csv file location delimiter ',' csv header;` to get around permissions issues. The tutorial link about was ok other than that. Specifying the columns in brackets after the table name is overkill for what I wanted too. – JGFMK May 11 '22 at 12:47