diff --git a/src/apk_package.h b/src/apk_package.h
index c6aa0511d191998be5e385f93cb2c83c056102f7..1d9ca6e56a785962dabea72cd3661175dff837f1 100644
--- a/src/apk_package.h
+++ b/src/apk_package.h
@@ -144,6 +144,7 @@ void apk_pkg_from_adb(struct apk_database *db, struct apk_package *pkg, struct a
 struct apk_installed_package *apk_pkg_install(struct apk_database *db, struct apk_package *pkg);
 void apk_pkg_uninstall(struct apk_database *db, struct apk_package *pkg);
 
+int apk_ipkg_assign_script(struct apk_installed_package *ipkg, unsigned int type, apk_blob_t blob);
 int apk_ipkg_add_script(struct apk_installed_package *ipkg,
 			struct apk_istream *is,
 			unsigned int type, unsigned int size);
diff --git a/src/app_mkpkg.c b/src/app_mkpkg.c
index 04ef697166000a25b0575a8327a5002fb23ff302..4e61066390909f22c027bb588397b4162fdd258b 100644
--- a/src/app_mkpkg.c
+++ b/src/app_mkpkg.c
@@ -74,7 +74,7 @@ static int option_parse_applet(void *ctx, struct apk_ctx *ac, int optch, const c
 	case OPT_MKPKG_script:
 		apk_blob_split(APK_BLOB_STR(optarg), APK_BLOB_STRLIT(":"), &l, &r);
 		i = adb_s_field_by_name_blob(&schema_scripts, l);
-		if (i == APK_SCRIPT_INVALID) {
+		if (!i) {
 			apk_err(out, "invalid script type: " BLOB_FMT, BLOB_PRINTF(l));
 			return -EINVAL;
 		}
diff --git a/src/database.c b/src/database.c
index 3776bb1480dfb08bc7f2e68565302eb552ed30ae..589a5052f6b3ce195d9903723804428cae0714c9 100644
--- a/src/database.c
+++ b/src/database.c
@@ -2334,10 +2334,19 @@ static int apk_db_install_v2meta(struct apk_extract_ctx *ectx, struct apk_istrea
 
 static int apk_db_install_v3meta(struct apk_extract_ctx *ectx, struct adb_obj *pkg)
 {
+	static const int script_type_to_field[] = {
+		[APK_SCRIPT_PRE_INSTALL]	= ADBI_SCRPT_PREINST,
+		[APK_SCRIPT_POST_INSTALL]	= ADBI_SCRPT_POSTINST,
+		[APK_SCRIPT_PRE_DEINSTALL]	= ADBI_SCRPT_PREDEINST,
+		[APK_SCRIPT_POST_DEINSTALL]	= ADBI_SCRPT_POSTDEINST,
+		[APK_SCRIPT_PRE_UPGRADE]	= ADBI_SCRPT_PREUPGRADE,
+		[APK_SCRIPT_POST_UPGRADE]	= ADBI_SCRPT_POSTUPGRADE,
+		[APK_SCRIPT_TRIGGER]		= ADBI_SCRPT_TRIGGER,
+	};
 	struct install_ctx *ctx = container_of(ectx, struct install_ctx, ectx);
 	struct apk_database *db = ctx->db;
 	struct apk_installed_package *ipkg = ctx->ipkg;
-	struct adb_obj triggers, pkginfo, obj;
+	struct adb_obj scripts, triggers, pkginfo, obj;
 	int i;
 
 	// Extract the information not available in index
@@ -2346,6 +2355,14 @@ static int apk_db_install_v3meta(struct apk_extract_ctx *ectx, struct adb_obj *p
 	ipkg->replaces_priority = adb_ro_int(&pkginfo, ADBI_PI_PRIORITY);
 	ipkg->v3 = 1;
 
+	adb_ro_obj(pkg, ADBI_PKG_SCRIPTS, &scripts);
+	for (i = 0; i < ARRAY_SIZE(script_type_to_field); i++) {
+		apk_blob_t b = adb_ro_blob(&scripts, script_type_to_field[i]);
+		if (APK_BLOB_IS_NULL(b)) continue;
+		apk_ipkg_assign_script(ipkg, i, apk_blob_dup(b));
+		ctx->script_pending |= (i == ctx->script);
+	}
+
 	apk_string_array_resize(&ipkg->triggers, 0);
 	adb_ro_obj(pkg, ADBI_PKG_TRIGGERS, &triggers);
 	for (i = ADBI_FIRST; i <= adb_ra_num(&triggers); i++)
diff --git a/src/package.c b/src/package.c
index 0ad015659fa588e28c4d2e32d81e250b067b156c..6003c5b87e812769bc603b59305dc6d817883ccc 100644
--- a/src/package.c
+++ b/src/package.c
@@ -738,20 +738,25 @@ void apk_pkg_free(struct apk_package *pkg)
 	free(pkg);
 }
 
-int apk_ipkg_add_script(struct apk_installed_package *ipkg,
-			struct apk_istream *is,
-			unsigned int type, unsigned int size)
+int apk_ipkg_assign_script(struct apk_installed_package *ipkg, unsigned int type, apk_blob_t b)
 {
-	apk_blob_t b;
-
-	if (type >= APK_SCRIPT_MAX) return -1;
-	b = apk_blob_from_istream(is, size);
 	if (APK_BLOB_IS_NULL(b)) return -1;
+	if (type >= APK_SCRIPT_MAX) {
+		free(b.ptr);
+		return -1;
+	}
 	if (ipkg->script[type].ptr) free(ipkg->script[type].ptr);
 	ipkg->script[type] = b;
 	return 0;
 }
 
+int apk_ipkg_add_script(struct apk_installed_package *ipkg,
+			struct apk_istream *is,
+			unsigned int type, unsigned int size)
+{
+	return apk_ipkg_assign_script(ipkg, type, apk_blob_from_istream(is, size));
+}
+
 void apk_ipkg_run_script(struct apk_installed_package *ipkg,
 			 struct apk_database *db,
 			 unsigned int type, char **argv)