{"id":2646,"date":"2015-09-21T01:44:29","date_gmt":"2015-09-21T01:44:29","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=2646"},"modified":"2015-09-21T01:44:29","modified_gmt":"2015-09-21T01:44:29","slug":"postgres-inserting-into-an-array","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/postgres-inserting-into-an-array\/","title":{"rendered":"Postgres: Inserting into an array"},"content":{"rendered":"<p>If you make an array column in Postgres, it&#8217;s not completely obvious how you&#8217;d insert static data into it.<\/p>\n<p>Say you create a table like so (two int array fields):<\/p>\n<pre lang=\"sql\">\ncreate table groups (\n  id int unique,\n  name varchar unique,\n  member_users int[],\n  member_groups int[]\n);\n<\/pre>\n<p>You can insert using quoted strings. Note that the quotes are apostrophes &#8211; otherwise Postgres interprets this as a column name (as if you were doing an &#8220;INSERT INTO table SELECT &#8230; FROM &#8230;&#8221;).<\/p>\n<p>Also note this uses &#8220;{}&#8221; braces (not [] like you&#8217;d expect on an array).<\/p>\n<pre lang=\"sql\">\ninsert into groups values (1, 'admins', '{1,2}', '{}');\n<\/pre>\n<p>If you&#8217;d like, you can also cast the text to an array (this doesn&#8217;t change the behavior, but you might find it clearer)<\/p>\n<pre lang=\"sql\">\ninsert into groups values (1, 'admins', '{1,2}'::int, '{}'::int[]);\n<\/pre>\n<p>You can also use spaces if you wish:<\/p>\n<pre lang=\"sql\">\ninsert into groups values (2, 'users', '{1, 2}', '{}');\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you make an array column in Postgres, it&#8217;s not completely obvious how you&#8217;d insert static data into it. Say you create a table like so (two int array fields): create table groups ( id int unique, name varchar unique, member_users int[], member_groups int[] ); You can insert using quoted strings. Note that the quotes &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.garysieling.com\/blog\/postgres-inserting-into-an-array\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Postgres: Inserting into an array&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[437,523],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2646"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=2646"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/2646\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=2646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=2646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=2646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}