Add custom fields to existing posts (admin pages)

As Andreas Krischer suggested, I fixed using an script to update all the old values and then only using the wp_insert_post to create the custom field for the new posts.

The script that I used:

INSERT INTO wp_postmeta( post_id, meta_key, meta_value ) 
SELECT wp_posts.ID,  'municipi',  ''
FROM wp_posts
WHERE wp_posts.post_status="publish"
AND wp_posts.post_type="post"

Here I’m updating all the posts that are with the status publish and are type post, and I’m creating a custom field called municipi with initial value empty.