Last active
April 24, 2019 07:36
-
-
Save Tuhaj/092020b99e5bc60971c82cc02b366427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example INSERT INTO with ON CONFLICT behavior | |
SAVED_FIELDS="state, name, type, instance_id, public_ip, launch_time, region, profile, publicdnsname" | |
row='{"state":"running","name":"important_key_name","type":"t2.medium","instance_id":"i-0a1b2c3d4e556677f","public_ip":"11.111.1.111","launch_time":"2019-01-30T08:00:00.000Z","region":"eu-central-1","profile":"","publicdnsname":"ec2-xx-xxx-x-x.eu-central-1.compute.amazonaws.com"}' | |
DATABASE="aws_instances" | |
TABLE_NAME="aws_ec2" | |
psql -c "INSERT INTO $TABLE_NAME($SAVED_FIELDS) SELECT $SAVED_FIELDS from json_populate_record(NULL::$TABLE_NAME, '${row}') | |
ON CONFLICT (instance_id) | |
DO UPDATE | |
SET state = EXCLUDED.state, | |
name = EXCLUDED.name, | |
type = EXCLUDED.type, | |
launch_time = EXCLUDED.launch_time, | |
public_ip = EXCLUDED.public_ip, | |
profile = EXCLUDED.profile, | |
region = EXCLUDED.region, | |
publicdnsname = EXCLUDED.publicdnsname | |
" -d $DATABASE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment