Where does the Media Library live in the database?

2018 / 02 / 26

The Media Library lives in both wp_posts and wp_postmeta.

  • wp_postmeta contains the image URL
  • wp_posts contains an entry for each image insertion into a post, along with the post ID.

Exporting and importing these 2 tables as SQL did not work for me – I received ‘duplicate entry for key 7’…

Exporting and importing these 2 tables as CSV did work, using “CSV using load data”.

Before importing, I emptied the 2 tables in the recipient database.

Select * from wp_posts where post_type = 'attachment';

Will return all the entries off media library. After the execution, you can export the result table as SQL or CSV or any other portable data format you like. Remember if you not sure the entries exist in database, use Insert IGNORE statement instead of INSERT. (Possible through exporting pan in phpMyAdmin or other MySQL clients). Also there are entries refer to media library for each post, such as attachment images or thumbnail images, witch is store in wp_postmeta table. WordPress store them so the media attaches to posts or pages. If you want them to be exported too, you will need to use something like this :

SELECT * FROM  `wp_postmeta` WHERE meta_key IN ('_wp_attached_file', '_wp_attachment_backup_sizes',  '_wp_attachment_metadata',  '_thumbnail_id');

And then you can export them to wherever you want. It is all I know about media library stuff in WordPress.


Hi, I am a full-stack web developer with 5+ years of experience in working with different web technologies. Do you want to ask something? just send me a message through the contact form. Please visit my portfolio at Thanks